Reputation: 2192
I am working on Entity frame work, i have created a method which is returning List of my Table, I am retrieving data on base of grpID(which is foreign key, so i can have multiple records) I have saved these grpID's in an array so I want to run IN command on Entity framework so that i can get records in single List, How can i apply In command,my code is below
public List<tblResource> GetResources(long[] grpid)
{
try
{
return dataContext.tblResource.Where(c => c.GroupId == grpid && c.IsActive == true).ToList();//This code is not working as i am having array of groupIds
}
catch (Exception ex)
{
return ex;
}
}
Upvotes: 0
Views: 193
Reputation: 14672
How about...
dataContext.tblResource.Where(c=> gripid.Any(GroupId)
&& c.IsActive == true).ToList();
Upvotes: 0
Reputation: 223247
You may use Contains to mimic something like Select IN
query
dataContext.tblResource.Where(c=> gripid.Contains(c.GroupId)
&& c.IsActive == true)
.ToList();
where grpid
is your array of IDs.
You may consider the following example. Suppose you have Product class with two properties ID and Name, and array of ProductList containing the IDs. The Select IN Query should be something like:
int[] productList = new int[] { 1, 2, 3, 4 };
List<Product> products = new List<Product>();
products.Add(new Product { ID = 1, Name = "Test" });
products.Add(new Product { ID = 2, Name = "Test" });
products.Add(new Product { ID = 6, Name = "Test" });
var myProducts = from p in products
where productList.Contains(p.ID)
select p;
var methodChainingQuery = products.Where(c => productList.Contains(c.ID));
Upvotes: 1
Reputation: 148120
Use IEnumerable.Contains with group id array.
List<tblResource> GetResources(long grpid)
{
try
{
return dataContext.tblResource.Where(c => grpIDArray.Contains(c.GroupId)
&& c.IsActive == true).ToList();
}
catch (Exception ex)
{
return ex;
}
}
Upvotes: 0