c# - 不支持错误

标签 c# linq linq-to-entities azure azure-table-storage

我有一个接受日期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/

相关文章:

c# - 如何检测隐藏的桌面?

c# - 如何向全局轴中的对象添加力?

c# - 在 Linq to NHibernate 中测试条件的最佳方法是什么?

c# - 如何返回流而不是写入磁盘?

c# - 如何在不跳过的情况下精确跟踪鼠标坐标

c# - Linq 返回没有跟进记录的记录

c# - LINQ 连接查询结构导致 CS0119 错误

c# - chain linq查询的顺序执行

c# - Linq 性能 : should I first use `where` or `select`

c# - 转换 Nullable DateTime 时,LINQ to Entities 无法识别方法 'System.String ToString()' 方法