score:9
The =
operator in fact does two things:
- Set the field/property on the left-hand side to the value on the right-hand side.
- Return the newly assigned value.
This is also the reason why statements like this work:
object item;
while ((item = getItem()) != null)
processItem(item);
score:3
It compiles because it's a valid Func<T, bool>
. There's no way for the compiler to tell that in this case, it shouldn't allow that.
score:4
x => x.SomeBooleanProperty = true
This lambda means - for x, assign true
to SomeBooleanProperty
. The result of the assignment is the value true
as well.
If this is changed to:
x => x.SomeBooleanProperty
The lambda means - for x return the value of SomeBooleanProperty
.
score:0
Looks like the problem is related to the fact that
x => x.BooleanProperty = true
evaluates to true and is thus a valid where() predicate
I tried it with ints and was able to get the same behaviour.
[TestMethod]
public void Test_because_im_scared() {
var falseProperty = new TestModel { BooleanProperty = false };
var trueProperty = new TestModel { BooleanProperty = true };
var list = new List<TestModel> { falseProperty, trueProperty };
var results = list.Where(x => (x.IntProperty = 17) == 17) ;
Assert.IsTrue(list.All(itm => itm.IntProperty == 0));
//all fine so far, now evaluate the results
var evaluatedResults = results.ToList();
Assert.IsTrue(list.All(itm => itm.IntProperty == 0)); // fails here, all 17
}
private class TestModel {
public bool BooleanProperty { get; set; }
public int IntProperty { get; set; }
}
AFAIK this is unintended behaviour, the IEnumerable<> extensions should all return new enumerables and not change the original but I have not seen that guaranteed anywhere.
It looks like it could be used as a pseudo foreach() but I wouldn't recommend it :-/
Alan.
Source: stackoverflow.com
Related Articles
- Why does IEnumerable.Where method allow a predicate that can change the data?
- The data source does not support server-side data paging
- Expression references a method that does not belong to the mocked object
- Is there an IEnumerable implementation that only iterates over it's source (e.g. LINQ) once?
- C# method accepting a predicate - does this look ok?
- LINQ WHERE method alters source collection
- Passing IEnumerable data from LINQ as parameter to a method
- How does method overload resolution work (LINQ Where extension method)?
- How does linq actually execute the code to retrieve data from the data source?
- How does this linq code that splits a sequence work?
- Add a LINQ or DBContext extension method to get an element if not exist then create with data in predicate (FirstOrCreate)
- Is there a way to speed up this code that finds data changes in two XML files?
- Linq + Where + abstract method = LINQ to Entities does not recognize method
- Is there a Linq method that does .SelectMany(x => x)?
- Nhibernate linq. The where extension method does not add the where clause to the SQL command, why?
- How can I have a LINQ where clause that does nothing?
- How does the Where method run?
- Does a code that combines single() with yield make any sense?
- Can I use a LINQ IEnumerable result as the data source for a Gtk.TreeView?
- Task Does Not Contain a Definition for Where If Done in One Line of Code
- Why the extension method of where for LINQ in this code would print out a single number while it shouldn't print anything at all?
- Convert code that uses Linq to code that does not
- How to make a method that returns an IEnumerable of anonymous types
- Linq extension method equivalent for that code in VB.NET
- Change boolean value of some data where date == DateTime.Today
- Union 2 lists with additional content change for ID that occurs in both source lists
- C#--How do I use a Null Coalescing operator to allow use of an extension method that won't accept null parameters?
- Where Linq Method returning elements that don't satisfy condition
- Why does Resharper suggest a code change and then complain about the change?
- Show Data to dropdownlist - "LINQ to Entities does not recognize the method 'System.String ToString()' method"
- Linq to entities in asp.net and generic delete method
- How do I convert this double foreach loop to just lambdas?
- Convert SQL to LINQ Query
- How to remove duplicates from a list of custom objects, by a property of the object
- Converting IEnumerable<T> to List<T> on a LINQ result, huge performance loss
- Extension method not updating object passed in
- In C# how do access my LINQ var array from another method?
- EF Core, append to predicate builder in Any condition
- How to retrieve a single record using a foreign key column in LINQ?
- how to get the row value of each column from linq
- IEnumerable<IEnumerable<int>> - no duplicate IEnumerable<int>s
- Need a LINQ query to find string items
- How to sort a list, saving map from old positions to new positions?
- Multiple Linq functions as parameter
- Linq OrderByDescending but keep zero value first
- Merge Lists and return only duplicates
- Linq record not in other table
- Group elements by an array/Collection<T>
- How to find first occurrence with LINQ
- Convert simple Left Outer Join and group by SQL statement into Linq