0

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;
            }
        }
0

3 Answers 3

1

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));
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for reply,but i am having long array, so i can't apply Contains :(
you can apply Contains with long data type. :) it is not just for strings.
0

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;
     }
 }

2 Comments

thanks for reply,but i am having long array, so i can't apply Contains :(
Did you try?, You can apply to long as well.
0

How about...

dataContext.tblResource.Where(c=> gripid.Any(GroupId) 
                                && c.IsActive == true).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.