locked
creating an ef query on an array RRS feed

  • Question

  • User-2097295820 posted

    I have a Poco class called Product. I also have an array which contains ids of Products which I would love to find. I want a way I can write a single query that would help find Products based on the ids in the int  array.

    I am trying to do it in the best posible way. I dont want to use some unneccessary loops.

    this.context.Products.Where (item=>item.ProductID == ........ ids in the array woould come here  ......).AsEnumerable();

    Please how do I get this to work. Thanks for reading.

    Sunday, March 5, 2017 10:33 PM

All replies

  • User2103319870 posted

    skliz4rel

    . I also have an array which contains ids of Products which I would love to find. I want a way I can write a single query that would help find Products based on the ids in the int  array.

    I am trying to do it in the best posible way. I dont want to use some unneccessa

    You can use the contains like below

    //Your sample array
    int[] idArray = { 1, 2, 3 };
    this.context.Products.Where(item => idArray.Contains(Convert.ToInt32(item.ProductID))).AsEnumerable();
    Monday, March 6, 2017 12:02 AM
  • User-832373396 posted

    Hi skliz4rel,

    Maybe you could use Find method to achieve this:

    this.context.Products.Find(1,2,3);  //that means key values in(1,2,3)

    Best regards,

    Jolie

    Monday, March 6, 2017 7:54 AM