SQL “not in” syntax for Entity Framework 4.1

前端 未结 4 2094
别跟我提以往
别跟我提以往 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条回答
  •  独厮守ぢ
    2020-12-15 05:05

    This might be a way to do what you want:

    // From the method you provided, with changes...
    public static List GetLists(int[] ids) // Could be List or other =)
    {
        using (dbInstance db = new dbInstance())
        {
            return db.Lists.Where(m => !ids.Contains(m.ID));
        }
    }
    

    However I've found that doing so might raise error on some scenarios, specially when the list is too big and connection is somewhat slow.

    Remember to check everything else BEFORE so this filter might have less values to check.

    Also remember that Linq does not populate the variable when you build your filter/query (at least not by default). If you're going to iterate for each record, remember to call a ToList() or ToArray() method before, unless each record has 500MB or more...

提交回复
热议问题