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: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.
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();
}
Source: stackoverflow.com
Related Query
- How to order a List by the order of another List?
- how to sort a List based on the form order in another List
- C# How to sort a list base on the order of another list
- in C#, how do I order items in a list where the "largest" values are in the middle of the list
- How to order a list by property and group the elements in HTML Razor?
- How do I use Linq to find the elements of a list that are not present in another list?
- how split the List of strings into another Lists if particular string matches condition using linq?
- How can I compare two Lists and create another list where the match?
- How to get the list elements that are not into another list C#
- Why EF can't sort items by the order of another collection and how to workaround?
- How to sort the list order using Linq?
- How to add Count if the duplicate index is occur when add list to another list?
- How to get a list which contains at least all the values of another list?
- Using Linq to object, how can I get one field's value based on another in the same list
- How to fetch a list from the database based on another list of a different object type?
- How to filter linq query based on all of the values of one property from another list
- How can I update the contents of a list with data from another list?
- How to use LINQ to find all items in list which have the most members in another list?
- How to order by C# list so that the duplicate items appear in the reverse order of their insertion
- How to order the items of a list and remove duplicates based on a property?
- Using LINQ. How can I compare two simple lists to give another list of objects in the new and not in the old list?
- How to search in a list whether a list of given items exists in the same order
- How to sort a list with the values of another list with Linq
- How to filter the list using another list?
- How to distribute a list of objects in a new reorganized list of objects having an embedded list in order to change the root primary key/index?
- How can I take a list of dates, and remove any that are within 1 minute of another date in the list with LINQ?
- How to efficiently filter elements in the List based on values in another list
- In C#, how can I order a list after its grouped by size of the list?
- How to write a query in order to achieve the orderby descending clause for an uninitialised generic collections such as List of integers in c#?
- How to OrderBy() as per the order requested in distinct string list
More Query from same tag
- EF Core Include childs after query with joins
- Linq equivalent statement for SQL Inner Join with IN statement
- Can this LinQ statement made run multi threading - using more cpu cores
- Linq not in select on datatable
- Fetch all foreign key records from collection
- EntityFrameworkCore Linq error: variable 'country.Country' of type 'Country' referenced from scope '', but it is not defined
- Select Numeric Values From String Linq to Entities
- How do I use LINQ to Enumerate Dictionaries
- how to use linq to get dictionary object's variable
- EF Core ThenInclude foreign key values with 1:n relationshop
- Entity Framework ORDER BY issue
- Using Linq to get unique entries
- How to reference a specific object from a list in a config file using .NET MVC with C#
- What is the time complexity of Linq OrderBy().ThenBy() method sequence?
- How to Add to an ObservableCollection<T> Item with LINQ Where() in Getter?
- How to group by with only Time format in Linq
- SQL generated from LINQ not consistent
- join table and get the record which has minimum value?
- Trouble with using predicatebuilder in a foreach loop
- LINQ query, group by and multiple sums
- Linq where clause with DbFunctions.DiffDays and string comparison not working
- Get refreshed data using LINQ to SQL
- Translate an SQL Query to Linq to Entities
- How to add OrderBy clause in multi-table LINQ query?
- C# Linq query as a function parameter
- Dynamic table name in linq
- Databinding to repeate dynamic object creation
- Dynamically query entity framework with linq
- Invoking Generic Method with the parameter of IEnumerable<T>
- Unit test IObservable<T> with ObserveOnDispatcher