在LINQ查询上使用count时,Sequence不包含任何元素

时间:2014-11-14 08:33:54

标签: linq sequences

IEnumerable<EntityReference> children = (
    from c in OrgContext.CreateQuery<Account>()
    where
        c.ParentAccountId != null && c.ParentAccountId.Id.Equals(parentGuid)
    select 
        c.ToEntityReference()).ToList();

return children.Count() > 0 ? null : new List<EntityReference>();

引发&#34;序列不包含任何元素&#34;错误。在尝试计算元素之前,如何可靠地检查序列是否为空? (我希望Count只返回0)

1 个答案:

答案 0 :(得分:1)

这个错误对我来说是由使用旧版本而不是我当前构建的visual studio引起的。我不知道为什么会这样做,但我已经在2012年和2013年的visual studio中发生了这件事。只需删除bin文件夹即可修复它。

在您的情况下,我认为问题在于您使用Count()方法而不是Count属性。