score:4
The following worked:
public List<MyType> GetMyTypes(List<int> ids)
{
var unordered = (from myType in db.MyTypes
where ids.Contains(myType.Id)
select new MyType
{
MyValue = myType.MyValue
}).ToList();
var ordered = (from uo in unordered
orderby ids.IndexOf(uo.Id)
select uo).ToList();
return ordered;
}
score:7
EDIT: now that the mistake I made in understanding the requirement has been pointed out, I suggest this as a more performant method of achieving the desired result:
public static List<MyType> GetMyTypes(List<int> ids)
{
int index = 0;
Dictionary<int, int> positions = ids.ToDictionary(c => c, c => index++);
MyType[] results = new MyType[ids.Count];
foreach (MyType aType in (from myType in db.MyTypes
where ids.Contains(myType.Id)
orderby myType.Id
select myType))
{
results[positions[aType.Id]] = aType;
}
return results.ToList();
}
This won't do a search through the ids list for every element in db.MyTypes (which is a good thing: it'll be fast!).
My original (incorrect) answer:
Use an orderby clause.
public List<MyType> GetMyTypes(List<int> ids)
{
return (from myType in db.MyTypes
where ids.Contains(myType.Id)
orderby myType.Id
select new MyType
{
MyValue = myType.MyValue
}).ToList();
}
It's not clear what type of object db.MyTypes returns but, at a guess, the code could be simplified a little by avoiding the newing up of more MyType objects.
public List<MyType> GetMyTypes(List<int> ids)
{
return (from myType in db.MyTypes
where ids.Contains(myType.Id)
orderby myType.Id
select myType).ToList();
}
score:5
Daniel is nearly right, but an easy way to use the order of the incoming list is to order by the index of the ID in that list:
public List<MyType> GetMyTypes(List<int> ids)
{
return (from myType in db.MyTypes
where ids.Contains(myType.Id)
orderby ids.IndexOf(myType.Id)
select myType).ToList();
}
Note I don't know if that will work with a SQL query, so this may be Linq-to-objects only. It could also be very inefficient with large lists of ids.
Source: stackoverflow.com
Related Articles
- Check if one list contains all items from another list in order
- How to order a List by the order of another List?
- Copy a list of objects to another typed list in one line of code
- Order a list of objects by value match in another list
- GroupBy, then order another list based on group in C#
- Order one list with a parameter from another list, with a joinable parameter
- Order List in same order as another list
- Create list of dynamic objects with order and columns from another list
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- List order by another contained list
- how to sort a List based on the form order in another List
- List or Array of String Contain specific word in Html Source Code
- C# How to sort a list base on the order of another list
- Select from one list only those present in another and order by second list - linq
- c# Linq or code to extract groups from a single list of source data
- Linq query to order list by occurences of foreign key in another list
- How to set order of IQueryable query based on another list using linq in c#?
- Group list elements by parent Id, then order by another field
- Select item from one list based on item in another list in the same order
- Sort a list from another list IDs
- Adding List<t>.add() another list
- C# - code to order by a property using the property name as a string
- Sort one list by another
- Subtract a generic list from another
- LINQ query to find if items in a list are contained in another list
- LINQ - Find all items in one list that aren't in another list
- Remove items of list from another lists with criteria
- Using LINQ, select list of objects inside another list of objects
- How to filter a list based on another list using Linq?
- Find items from a list which exist in another list
- find all strings in string array from another string in a specific order mvc
- convert string in an array of objects
- Using into and from in Linq query
- Error :The ObjectContext instance has been disposed and can no longer be used for operations that require a connection
- How to search between two dates in LINQ to Entity?
- orderby certain property - code improvement
- Linq thenby running indefinitely
- Custom function for linq to sql
- LINQ Max extension method gives an error on empty collections
- Order a List<T> by Date, but it has a String ID, if that ID is repeated with different date in the collection with should appear below of it
- Error in Conversion from Generic List<Datarow> to Generic List<string>
- C# DataTable How to sort the solution after grouping?
- Check specific values in an array
- entity to linq where clause
- Aggregate with OrderBy and ThenBy
- LINQ: Group by Date Range within 5 minutes
- How to use LINQ to return a query result to a dictionary
- Creating Entity Frame work via model first in visual studio
- What requirements on type used in IEnumerable.GroupBy()?
- detect multiple values in column. Then loop through those values