2

Basically I have a list containing all items. Then I have a string containing the IDs I want to grab out from the list, to do this I split the string into an Int array and later use this and LINQ to get the items I want from the list

Like this :

List<T> lstAllList = Model.getAllItems();

string stringIDs = "8,9,12,11,7";

int[] intArray = stringIDs.Split(',').Select(n => Convert.ToInt32(n)).ToArray();

List<T> lstLimitedList = (from r in lstAllList where intArray.Contains(r.id) select r).ToList();

Which works great.

But the issue here is that I want to have my list ordered in the same way as the string of IDs is, i.e 8,9,12,11,7 like in this example. But the returned list from the LINQ sorts it by the id by default, so the returned list is 7,8,9,11,12.

Is there a way to prevent it from sorting it like this or is there a way to sort the new list with my int array?

5 Answers 5

3

Sure, just sort by the index of the ID in the array:

string stringIDs = "8,9,12,11,7";

int[] intArray = stringIDs.Split(',').Select(n => Convert.ToInt32(n)).ToArray();

var lstLimitedList = (
        from r in lstAllList 
        where intArray.Contains(r.id) 
        orderby Array.IndexOf(intArray, r.id)   // <--------
        select r).ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

Well this was easier than expected. Will accept the answer when it becomes available. Thanks!
1

Simply getting the elements one at a time may be faster than trying to resort. (Anyone willing to calculate the O() costs?)

List<T> lstLimitedList = new List<T>();

foreach(int id in intArray)
{
    lstLimitedList.Add(lstAllList.Where(item => item.id = id));
}

You could also use intArray.ForEach() if you're a LINQ maniac, but this is much easier to read. ;)

Comments

1

Try to rotate your query. Under word rotate I mean start with intArray and use join. Something like this:

List<T> lstLimitedList = (
from id in intArray 
join item in lstAllList on id equals item.Id 
select item).ToList();

Comments

0

Id use the intersect extension method with provided by LINQ!

int[] array ={ 8,9,12,11,7} // or your result from your split on string;
List<int> array2 = new List<int> { 8,9,12,11,7 } // your model.GetAllItems;

// Call Intersect extension method.
var intersect = array.Intersect(array2);
// Write intersection to screen.
foreach (int value in intersect)
{
    Console.WriteLine(value); // Output: 8,9,12,11,7 
}

Bit cleaner for me

Comments

-2

Stop overusing LINQ guys. In this case linq is a total overkill. A much simpler and better performance-wise solution is the following:

string a = "8,9,12,11,7";

List<int> list = new List<int>();

string[] splitted = a.Split(',');
for (int i = 0; i < splitted.Length; i++)
{
    list.Add(int.Parse(splitted[i]));
}

Which a single loop and without sorting etc.

3 Comments

So you are not even gonna defend your -1's against this working easy solution ?
The end result should be a list of Ts (whatever that is), not a list of ints.
The OP is trying to sort based on the order of the values in his array. You provided an alternative way of getting the array in the first place. You have not answered the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.