2010-12-20 29 views
0

我需要帮助写一个LINQ语句不一样的下面的SQL查询SQL ...LINQ的帮助下,在()语句

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

感谢

+0

这不是一个有效的SQL查询...编辑:对吗? – 2010-12-20 15:59:13

+0

对我来说没问题。 – 2010-12-20 16:01:39

+0

@Abe:你说得对,我现在明白了。 – 2010-12-20 16:02:08

回答

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
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 
        } 
+0

我最终做了这样的事情,因为我的列表中只有2个项目,所以我可以摆脱它。我有另一个具有不同列表的查询,所以其他答案也会派上用场。谢谢 – JBeckton 2010-12-20 16:35:40

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 };