Ayyash
Ayyash

Reputation: 4399

When Where clause is used inside Linq statement produces different results than when used outside

I have the following statement:

List<string> tracks = new List<string> { "ABC", "DEF" };
var items = (from i in Agenda.AgendaSessions
  select i).Where(p => p.Tracks.Any(s => tracks.Contains(s.Code)));

this returns all sessions which track contains either ABC or DEF, now when I rewrite the statement like the following, it returns All sessions regardless, as if the clause always yeilds into true, can anyone shed any light on this please?

var items = from i in Agenda.AgendaSessions
  where i.Tracks.Any(s=> tracks.Contains(s.Code))
  select i;

Update

if there are other clauses within the where, does that affect the results?

Upvotes: 4

Views: 195

Answers (1)

Timwi
Timwi

Reputation: 66573

The two code snippets are equivalent, i.e. they should always produce the same results under all circumstances. Of course, that assumes that AgendaSessions, Tracks and .Contains() are what we expect them to be; if they are property getters/methods which have curious side-effects such as modifying the contents of tracks, then anything could happen.

In other words, without knowing what the rest of your code looks like, we cannot help you, because there is no semantic difference between the two code snippets.

Upvotes: 2

Related Questions