LINQ to SQL in and not in

前端 未结 3 1577
醉梦人生
醉梦人生 2020-11-29 09:44

What is in and not in equals in LINQ to SQL?

For example

select * from table in ( ...)
and 
select * from table not in (.         


        
3条回答
  •  爱一瞬间的悲伤
    2020-11-29 10:10

    I'm confused by your question. in and not in operate on fields in the query, yet you're not specifying a field in your example query. So it should be something like:

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

    or

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

    The equivalent of those 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;
    

提交回复
热议问题