2

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?

1
  • db.MyForm1_hosps.Where(ta => ta.recordId == recordId); Commented Feb 20, 2012 at 16:56

1 Answer 1

9

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!

Sign up to request clarification or add additional context in comments.

1 Comment

derp derp, that is what i get for using a method based solely on its name. thanks for the explanation and change suggestion.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.