开发者

Linq help with SQL in() statement

开发者 https://www.devze.com 2023-01-31 06:09 出处:网络
I need help writing a Linq statement that does the same as the following sql query... select col1, col2, col3

I need help writing a Linq statement that does the same as the following sql query...

select col1, col2, col3
from table1
where col1 = 开发者_运维问答'foo'
and col2 = 0
and IsNull(col3, '') IN('A','')

thanks


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


The Contains operator is a special one, which you can say something along the lines of

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


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

精彩评论

暂无评论...
验证码 换一张
取 消