在LINQ查询上使用count时,序列不包含任何元素 [英] Sequence contains no elements when using count on LINQ Query

查看:127
本文介绍了在LINQ查询上使用count时,序列不包含任何元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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>();

抛出序列不包含任何元素"错误.如何在尝试计算元素之前可靠地检查序列是否为空? (我希望Count仅返回0)

Throws a "Sequence contains no elements" error. How can I reliably check if the sequence is empty before trying to count the elements? (I'd expect Count to simply return 0)

推荐答案

对我来说,此错误是由使用旧版本而不是当前版本的Visual Studio引起的.我不知道为什么会这样做,但是我已经在Visual Studio 2012和2013中发生过这种情况.只需删除bin文件夹即可解决问题.

This error for me was caused by visual studio using an old build instead of my current build. I don't know why it does this, but I've had it happen in visual studio 2012 and 2013. Just deleting the bin folder fixed it.

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

In your case, I think the problem is that you're using the Count() method instead of the Count property.

这篇关于在LINQ查询上使用count时,序列不包含任何元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆