我有一个接受日期dtSince
和字符串component
作为参数的方法。我需要编写一个单行 linq 查询,用于搜索 dtSince 之前发生的条目,并且属于组件 component
(如果指定)。我尝试过以下方法:
var result = from items in MyAzureTable
where items.Occured >= dtSince
&& items.Component == (component ?? items.Component)
select items;
但我收到了 NotSupported 错误。我猜 items.Component == (component ?? items.Component)
是问题所在。
如上所述,component
可以为 null 或为空。但我不能在原始查询中排除这一点,因为:
var result = from items in MyAzureTable
where items.Occured >= dtSince
select items;
可能返回超过 1000 行(这似乎是 Azure 表的默认限制),因此我稍后无法使用组件
对其进行过滤。如果我执行如下操作,我可能要查找的条目位于第 1001 行。因此,它不会给出我正在查找的结果。
if (!String.IsNullOrEmpty(component))
{
result = result.Where(x => x.Component == component).ToList<>();
}
问题:是否可以有一个单行 linq 查询,可以在在 where 子句中使用它之前先检查非空字符串?
最佳答案
试试这个:
var result = from items in MyAzureTable
where items.Occured >= dtSince &&
(component == null || items.Component == component)
select items;
关于c# - 不支持错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6261469/