peroija
peroija

Reputation: 1982

Linq returning unexpected results

I have the following query which should return no rows, but returns 4:

var testAgainst = db.MyForm1_hosps.Select(ta => ta.recordId == recordId);

If I use the following query I get zero rows as expected:

var testAgainst = from ta in db.MyForm1_hosps
                  where ta.recordId == recordId
                  select ta;

There ARE four rows in MyForm1_hosp but none match the recordId in my test.

The code doesn't lie, so my understanding of LINQ is incorrect. Can someone explain to me why the first one returns 4 rows when it should return 0?

Upvotes: 2

Views: 97

Answers (1)

Anthony Pegram
Anthony Pegram

Reputation: 126982

Your first query has a mistake. Change the Select to Where.

var testAgainst = db.MyForm1_hosps.Where(ta => ta.recordId == recordId); 

Investigating your original query with Select(ta => ta.recordId == recordId): This will return a sequence of booleans. If none of the records in db.MyForm1_hosps have recordId values matching the input, the results will all be false. If you have 4 records, you will get 4 false values. You have 4 results, but they are not of the type you think they are!

Upvotes: 9

Related Questions