I have the following query that receives a list of ints as a parameter:
public int GetMostRecent(List<int> TheIDs)
{
...using MyDC...
var TheMostRecentID = (from d in MyDC.Data
where TheIDs.Contains(d.ID)
orderby d.DateTime
select d.ID).LastOrDefault();
}
Is this the best way to match a list within a parameter collection to data in the database or is there a better way than using the .Contains() method in linq-to-sql.
Thanks.
What you have is correct. This will be translated into an
INclause in SQL with the values supplied in the collection.On an unrelated note, you should try ordering the query by date descending and use
FirstOrDefault(). As it is now, you’re going to bring back the entire result set and throw away every row but one.