Is there a C# IN operator?

后端 未结 14 1879
说谎
说谎 2020-12-08 03:51

In SQL, you can use the following syntax:

SELECT *
FROM MY_TABLE
WHERE VALUE_1 IN (1, 2, 3)

Is there an equivalent in C#? The IDE seems to

14条回答
  •  忘掉有多难
    2020-12-08 04:06

    Duplicate of : LINQ to SQL in and not in

    select * from table where fieldname in ('val1', 'val2') 
    

    or

    select * from table where fieldname not in (1, 2) 
    

    The equivalent of IN and NOT IN queries in LINQ to SQL would be something like this:

    List validValues = new List() { "val1", "val2"}; 
    var qry = from item in dataContext.TableName 
              where validValues.Contains(item.FieldName) 
              select item; 
    

    and this:

    List validValues = new List() { 1, 2}; 
    var qry = from item in dataContext.TableName 
              where !validValues.Contains(item.FieldName) 
              select item; 
    

提交回复
热议问题