将Linq'重写为其他格式'

时间:2011-03-03 19:15:36

标签: c# linq

是的,这是一个奇怪的标题,我知道,也许下面的文字可以帮助:

通常我们会像这样编写我们的linq(称之为'格式b'):

persons.where(z=> z.year > 200);

现在我已经在'format a'

中写了一个linq语句
act = from n in act
where ! (n.Ready && n.ReadyDateTime !=null && (DateTime.Now - n.ReadyDateTime.Value).Days > 30)
select n;

格式'b',我无法使用!运算符,所以我最终得到格式'a'

它工作正常,但我很好奇我怎么能用'格式b'

写它

恩我也很好奇我怎么称'格式'和'格式b':)

2 个答案:

答案 0 :(得分:4)

您的“格式b”称为方法调用。

要将查询理解语法转换为方法调用,请编写

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

答案 1 :(得分:1)

或者为了避免使用'!',只需反转查询:

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