How to get List of results from list of ID values with LINQ to SQL?
Posted
by DaveDev
on Stack Overflow
See other posts from Stack Overflow
or by DaveDev
Published on 2010-04-28T23:57:25Z
Indexed on
2010/04/29
0:07 UTC
Read the original article
Hit count: 274
I have a list of ID values:
List<int> MyIDs { get; set; }
I'd like to pass this list to an interface to my repository and have it return a List that match the ID values I pass in.
List<MyType> myTypes = new List<MyType>();
IMyRepository myRepos = new SqlMyRepository();
myTypes = myRepos.GetMyTypes(this.MyIDs);
Currently, GetMyTypes() behaves similarly to this:
public MyType GetMyTypes(int id)
{
return (from myType in db.MyTypes
where myType.Id == id
select new MyType
{
MyValue = myType.MyValue
}).FirstOrDefault();
}
where I iterate through MyIDs and pass each id in and add each result to a list.
How do I need to change the LINQ so that I can pass in the full list of MyIDs and get a list of MyTypes out? GetMyTypes() would have a signature similar to
public List<MyType> GetMyTypes(List<int> myIds)
© Stack Overflow or respective owner