问题
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