Tracy Tracy - 2 months ago 11
C# Question

IEnumerable .Select() Not Working

Can someone see what I'm doing wrong below? The first command returns an empty list. Then the second command sets i = 1 (which is correct).

IEnumerable<Demographic> demographics2 = this
.DemographicRecords
.Where(item => item.Id == "7633")
.Select(item => item);

int i = this
.DemographicRecords
.Where(item => item.Id == "7633")
.Count();

Answer

The first command returns an empty list.

No, it returns a query. You need to process the query via a foreach loop or a call to ToList or ToArray to get to the actual results of the query. Evaluation functions like Any(), Count(), Max() etc. also process the query since they return a concrete result and not another query.

I suspect you are looking at the variable in the debugger. Looking at Linq queries in the debugger is notoriously tricky since the query is often times just a wrapper around some concrete collection.