none
Don't know how to write predicate to use an array not single value. RRS feed

  • Question

  • Hi all,

    Sorry for topic title. but I can't formulate better.

    I have an array with ID's. I want to get from DB all entries with those ID's.

    int[] arrayIDs;

    result = new List<object>().Where(t => t.Id ?????? arrayIDs);

    Is it possible to not call this for each id but retrieve it by one call? Like "in" in SQL.

     

    Thank you.

    Tuesday, April 20, 2010 4:29 PM

Answers

All replies

  • Where(t => arrayIDs.Contains(t.Id));
    • Marked as answer by visert Wednesday, April 21, 2010 6:32 AM
    Tuesday, April 20, 2010 5:54 PM
  • Hi again,

    There showed a problem with "Contains" method. Don't know what realy were changed and by who in the project, but now I'm getting the following error:

    LINQ to Entities does not recognize the method 'Boolean Contains[Int32](System.Collections.Generic.IEnumerable`1[System.Int32], Int32)' method, and this method cannot be translated into a store expression."

    the code:

    var context = new BlalaDBEntities();

    int[] ids = getIds();

    List<object> list = context.objectNames.Where(t => ids.Contains(t.Id));

    Please tell me if you have any ideas?

    Thank you.

    Monday, May 3, 2010 3:03 PM
  • It works if I convert IQueryable to List:

    List<object> list = context.objectNames.ToList().Where(t => ids.Contains(t.Id)).ToList();

    Will it slow my application if there will be big amount of data ?

    Wednesday, May 5, 2010 8:05 AM