Sequence contains no elements when using count on LINQ Query

1k views Asked by At
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

There are 1 answers

0
Josiah On

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.