Linq帮助SQL in()语句

时间:2010-12-20 15:55:38

标签: linq

我需要帮助编写一个与以下sql查询相同的Linq语句...

select col1, col2, col3
from table1
where col1 = 'foo'
and col2 = 0
and IsNull(col3, '') IN('A','')

感谢

3 个答案:

答案 0 :(得分:3)

from t in context.table1
where
   t.col1 == "foo"
&& t.col2 == 0
&& (new string[]{"A", string.Empty}).Contains(t.col3.DefaultIfEmpty(string.Empty))
select new {t.col1, t.col2, t.col3};

答案 1 :(得分:2)

包含运算符是一个特殊的运算符,您可以按照

的说法进行操作
List<string> col3s= new List<string>() { "A", "B" };

from c in table1
where c.col1 = "foo" && && c.col2 = 0 && col3s.Contains(c.col3)
select new { c.col1, c.col2, c.col3 };

答案 2 :(得分:1)

var q = from row in YourTableObject
        where row.col1 = "foo"
              && row.col2 = 0
              && (string.IsNullOrEmpty(row.col3) || row.col3 == "A")
        select new {
                       col1,
                       col2,
                       col3
                    }