Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I need to change the order of a result of the linq query based on the integer type generic list.
ex :

Linq i am using

var result= contect.tablename.where(somecondition).Tolist();
This result contains one code id which is an integer value.

I have another list

List<int> sororder=new list<int>();
sortorder.add(7);
sortOrder.add(9);


Now i need to sort result based on the above integer list.

Any help?
Posted

Use simply

var result = contect.tablename.where(somecondition).orderby(i=>i).Tolist();
 
Share this answer
 
Comments
Sowjanya49183 4-Dec-13 8:54am    
If i implement the above code then , the orderby is working only for the first item in the integer list.
Suppose my query result is {7,12}

Integer list is {7,3,4,12}
This case it is orders as expected.

If my query result is {3,12}
then the order by is not working .Based the integer list after 3 only 12 should get display.
Moumit Mondal 5-Dec-13 0:31am    
If your list is {5,8,13,1,7,90,3} then the result will be {1,3,5,7,8,13,90} ... did this you want(??) or give me your expected result on my example ...
Sowjanya49183 5-Dec-13 1:13am    
No.My Resulr is {1,3,5,7,8,13,90} and the list is {5,8,13,1,7,90,3} so based on the list result should sort in order {5,8,13,1,7,90,3} .
Moumit Mondal 5-Dec-13 6:07am    
Then why you want it orderby ... You already have ur desire result
"the list is {5,8,13,1,7,90,3} so based on the list result should sort in order {5,8,13,1,7,90,3} ."
Sowjanya49183 5-Dec-13 8:42am    
No i don't have the desire result. My expected result is that, now my problem how to do that sort after the linq.
My opinion is that this is a case where using Linq ... while perhaps possible ... is probably "over-kill" because: what you are really doing is quite arbitrary. If I understand your goal, it is to move entries in a query result that match entries in another list to the front of the query result.

For the case that your query result doesn't contain duplicate values, you can use something like this:
C#
private List<int> queryList = new List<int> {11, 42, 65, 33, 3, 5, 65, 5, 8};
        
private List<int>sortByList = new List<int> {65, 123, 42, 11};

int sortByInt;
int matchAt;

// somewhere in a method ...

// traverse the sortByList from back to front
// based on the assumption the desired result
// is a sort based on magnitude
for (int i = sortByList.Count - 1; i >= 0; i--)
{
    sortByInt = sortByList[i];

    if (queryList.Contains(sortByInt))
    {
        matchAt = queryList.IndexOf(sortByInt);
        queryList.RemoveAt(matchAt);
        queryList.Insert(0, sortByInt);
    }
}
That would handle the case of having entries in the sort-by list that do not occur in the query list.
 
Share this answer
 
v2
By comparing the Integer list with Linq result set (with key element) added the data to the new list and used this new list for further process. This resolved my problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900