Sequence contains no elements when using count on LINQ Query

妖精的绣舞 提交于 2019-12-11 20:50:44

问题


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

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)


回答1:


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.

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



来源:https://stackoverflow.com/questions/26925869/sequence-contains-no-elements-when-using-count-on-linq-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!