score:4

Accepted answer

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.


Related Articles