none
How to query entities with Any/Contains which their IDs need to match the ones inside a List?

    Question

  • Hello everyone,

     

    Basically I would like to load all the orders where the userID equals the ones from the userIDList so I tried like this:

     

    Code Snippet

    List<Guid> userIDList = ...;

     

    var query1= from so in context.SalesOrder

               where userList.Contains(so.UserID)

               select so;

     

     

     

    But it this is the error that im getting:

     

    base {System.SystemException} = {"LINQ to Entities does not recognize the method 'Boolean Contains(System.Guid)' method, and this method cannot be translated into a store expression."}

     

    I found this old thread:

     

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2678297&SiteID=1

     

    and I ended up doing this:

     

    Code Snippet
    var
    query1 = this.context.SalesOrder.Where(BuildContainsExpression<SalesOrder, Guid>(e => e.UserID, userIDList));

     

     

     

    That works fine! Now, is there a better way to do it with EF V1?

     

    Thanks

    Monday, October 27, 2008 2:07 AM

Answers

  • Hello Federico,

     

    As soon as userIDList exist only in memory in the client, the approach Colin suggests in that thread is the best approach I know of for LINQ to Entities in .NET 3.5 SP1.

     

    If userIDList actually exists in the database, it is possible to rewrite this query using the Any operator.

     

    We are considering adding some level of support for Contains in a future version.

     

    Diego

    Sunday, November 30, 2008 1:03 PM
    Moderator

All replies

  • Hello Federico,

     

    As soon as userIDList exist only in memory in the client, the approach Colin suggests in that thread is the best approach I know of for LINQ to Entities in .NET 3.5 SP1.

     

    If userIDList actually exists in the database, it is possible to rewrite this query using the Any operator.

     

    We are considering adding some level of support for Contains in a future version.

     

    Diego

    Sunday, November 30, 2008 1:03 PM
    Moderator
  • When you say future version, you meant v2 correct? Because this is an important feature of querying which cannot be ignored and should be fairly easy and linq to sql offers..

     

    Zeeshan

     

    Monday, December 01, 2008 3:54 PM
  • Hi Zeeshan,

    Yes, we agree this is a very useful feature in queries and the team is working on adding support for it in the next version (usual disclaimers apply).

    Thanks,
    Diego
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Saturday, December 20, 2008 12:07 PM
    Moderator
  • Hello Federico,

     

    As soon as userIDList exist only in memory in the client, the approach Colin suggests in that thread is the best approach I know of for LINQ to Entities in .NET 3.5 SP1.

     

    If userIDList actually exists in the database, it is possible to rewrite this query using the Any operator.

     

    We are considering adding some level of support for Contains in a future version.

     

    Diego

    Can you give me an exampl of doing this through the any operator?
    What is the difference between any and where ?

    Thanks
    Martin
    Thursday, April 30, 2009 10:55 AM