SQL “not in” syntax for Entity Framework 4.1

前端 未结 4 2103
别跟我提以往
别跟我提以往 2020-12-15 04:32

I have a simple issue with Entity Framework syntax for the \"not in\" SQL equivalent. Essentially, I want to convert the following SQL syntax into Entity Framework syntax:<

4条回答
  •  萌比男神i
    2020-12-15 04:59

    Give this a go...

    public static List GetLists(List listIDs)
    {
        using (dbInstance db = new dbInstance())
        {
            // Use this one to return List where IS NOT IN the provided listIDs
            return db.Lists.Where(x => !listIDs.Contains(x.ID)).ToList();
    
            // Or use this one to return List where IS IN the provided listIDs
            return db.Lists.Where(x => listIDs.Contains(x.ID)).ToList();
        }
    }
    

    These will turn into approximately the following database queries:

    SELECT [Extent1].*
    FROM [dbo].[List] AS [Extent1]
    WHERE  NOT ([Extent1].[ID] IN ())
    

    or

    SELECT [Extent1].*
    FROM [dbo].[List] AS [Extent1]
    WHERE  [Extent1].[ID] IN ()
    

    respectively.

提交回复
热议问题