Tracy Tracy - 11 months ago 73
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
.Where(item => item.Id == "7633")
.Select(item => item);

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download