score:11
You're being bitten by a captured variable:
var query = color.Where(c => c.Contains(s));
Lifts s
into a closure and reads the value of s
at the time of execution. In this case, this happens after you re-assign s
to something else.
What you end up having as your query is:
var query = color.Where(c => c.Contains(s)).Where(c => c.Contains(s));
Rather than what you're probably expecting:
var query = color.Where(c => c.Contains("g")).Where(c => c.Contains("a"));
This will produce the result you're expecting:
string s = "g";
string[] color = { "greena", "browna", "bluea" };
var query = color.Where(c => c.Contains(s));
Console.WriteLine(query.Count());
var b = "a";
query = query.Where(c => c.Contains(b));
Console.WriteLine(query.Count()); // <-- This is where the entire expression is evaluated
score:0
Your colors aren't spelled correctly.
Greena, browna, bluea all have the character 'a' in them. Your linq expression is working as intended.
score:0
Because firstly you are looking for letter 'g', which only 'greena' contains. Afterwards, you change the value of s = "a"
and then run a query and look for all words with letter 'a' in them, in this case all 3 of them have 'a', therefore you are getting 1 and 3 as an output. Everything is correct. Why are you intending to get 1 and 1?
score:0
On the first part, it says, you query all colors with the letter g inside which is only 1, that's why it outputs 1:
string s = "g";
string[] color = { "greena", "browna", "bluea" };
var query = color.Where(c => c.Contains(s));
On the second part, it says, you query all colors which contains letter a inside which is every index on your array has letter a inside so it outputs 3:
s = "a";
query = query.Where(c => c.Contains(s));
Console.WriteLine(query.Count());
score:3
The first line for the query:
var query = color.Where(c => c.Contains(s));
Does not put the results in the query. It generates a query that has a source of string[] color with a predicate (filter) of .Contains(s). This doesn't get executed until .Count() is executed.
What this means is on the next execution of query.Contains() it's working on the original source of items. So, while you would expect the first result:
s = "g";
color.Where(c => c.Contains(s));
to return a count of 1 which is "greena", and then
s = "a";
query.Where(c => c.Contains(s)); // Where query now contains only: "greena" and hence return 1 for a count
What's really happening is this:
s = "g";
string[] color = { "greena", "browna", "bluea" };
query = color;
Console.WriteLine(query.Where(c => c.Contains(s)).Count());
// Outputs 1 because g appears only in greena
s = "g";
// query still contains the original color list
Console.WriteLine(query.Where(c => c.Contains(s)).Count());
// Outputs 3 because a appears in all three
In order to function as you are expecting you have to force the execution of the linq in the first query:
string s = "g";
string[] color = { "greena", "browna", "bluea" };
var query = color.Where(c => c.Contains(s)).Select(x => x).ToArray();
// Notice the ToArray() -- it forces execution of the linq which returns the results, not the query itself.
Console.WriteLine(query.Count());
s = "a";
var query2 = query.Select(x => x).Where(c => c.Contains(s));
Console.WriteLine(query2.Count());
Console.ReadKey();
Source: stackoverflow.com
Related Articles
- Unexpected output of LINQ Where when filtering by Contains(variable)
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- linq where clause when id is in an array
- Why no intellisense when LINQ statement has no where clause?
- LINQ WHERE method alters source collection
- Where can I view LINQ source code?
- When using a LINQ Where clause on a Dictionary, how can I return a dictionary of the same type?
- LINQ where condition filtering
- Return an empty collection when Linq where returns nothing
- How to reuse a linq expression for 'Where' when using multiple source tables
- Where and When to use LINQ to Objects?
- Avoiding code repetition when using LINQ
- When to use lambda expressions instead of a Where clause in LINQ
- LINQ Source Code Available
- Linq with where clause in many-to-many EF Code First object
- LINQ query null exception when Where returns 0 rows
- Simplifying linq when filtering data
- Multiple level where clause filtering with Linq
- When Where clause is used inside Linq statement produces different results than when used outside
- Include where clause on linq query when param is not null Npgsql
- LINQ query returns old results when source list is re-initialized
- C# - Linq optimize code with List and Where clause
- Filtering by Where clause only when condition is not null
- Using LINQ when my where clause will contain a LIST
- Linq filtering results with multiple Where clauses
- creating Linq to sqlite dbml from DbLinq source code
- LINQ operater Where not supporting any of the members when used inside generic method
- C# - Code supposed to be unreachable when calculate value in LINQ sum method
- Linq sub query when using a repository pattern with EF code first
- LINQ unexpected behavior when returning IEnumerable and calling ToArray
- How to use LINQ to get unique columns from a DataTable
- Rebuild Parent->Child->GrandChild hierarchy with LINQ performance
- How to create a new DateTime where condition using table field with LINQ
- How to cause EF to build update SQL with string concat?
- Simple LINQ query
- LINQ: Condition in table and in related table
- Using LINQ's SkipWhile over string properties
- LINQ to Entities does not recognize the method 'Boolean Exists(System.Predicate`1[Entities.Connection])' method
- Convert SQL to LINQ Troubles
- Can a linq table be added to the asp .net cache?
- Anonymous Type, and the great unknown
- Finding property differences in two lists
- how to retrieve data from many to many relationship in mvc c# with linq
- More efficient or shorter way to write this code?
- LINQ to Entities : "select new" expression tree throws System.NotSupportedException
- Linq function like .Net string.CompareOrdinal
- Linq Group by, Concatenate and load child entities on a list
- What is the purpose of using "Select" multiple times in this LINQ query?
- XDocument reading child elements
- Executing stored procedure using .dbml file LINQ