开发者

rewrite Linq 'to other format'

Yep, this is a weird title, i know, maybe the text below helps:

normally we write our linq like this (call this 'format b'):

persons.where(z=> z.year > 200);
开发者_如何学Python

now i have written a linq statement in 'format a'

act = from n in act
where ! (n.Ready && n.ReadyDateTime !=null && (DateTime.Now - n.ReadyDateTime.Value).Days > 30)
select n;

In format 'b', i couldn't use the ! operator, so i ended up with format 'a'

It works allright, but i am curious how i can write it in 'format b'

En i'm also curious how i call 'format a' and 'format b' :)


Your "format b" is called Method Calls.

To convert your query comprehension syntax to method calls, write

act.Where(n => !(n.Ready && n.ReadyDateTime != null
            && (DateTime.Now - n.ReadyDateTime.Value).Days > 30));


Or to avoid the need for '!', just reverse the query:

act = from n in act
where (!n.Ready || n.ReadyDateTime ==null || (DateTime.Now - n.ReadyDateTime.Value).Days <= 30)
select n;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜