none
Using the .Contains method RRS feed

  • Question

  • I want to search my database for entries that appear within a list. Foe example. I want to search my table for all items that have a code 01, 02, 03. In SQL I would use the WHERE IN clause. How can I use this in LINQ. Here is asnippet of LINQ I am using

    Dim qry = From c in DB.MyTable

                   Where c.Code = "01" Or c.Code = "02" 'this is where I would like to use the equivalent of the WHERE IN clause

    Tuesday, July 27, 2010 4:26 PM

Answers

  • List<string> codes = new List<string> { "01", "02", "03" };
    var q = from t in dc.SomeTable where codes.Contains(t.Code) select t;

    ...or...

    List<string> codes = new List<string> { "01", "02", "03" };
    var q = dc.SomeTable.Where(t => codes.Contains(t.Code));


       Kristofer - Huagati Systems Co., Ltd.
    Cool tools for Linq-to-SQL and Entity Framework:
    huagati.com/dbmltools - add-in with new useful features for the L2S and EF designers in VS2008 and VS2010
    huagati.com/L2SProfiler - Query profiler for Linq-to-SQL, Entity Framework v4, and LLBLGen Pro
    Wednesday, July 28, 2010 3:53 AM
    Answerer

All replies