2010-12-20 25 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 };