Tracy
Tracy

Reputation: 670

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

Upvotes: 1

Views: 2706

Answers (1)

D Stanley
D Stanley

Reputation: 152644

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.

Upvotes: 10

Related Questions