I’m a bit confused why this piece of code produces 2 records in a list (acts appropriately):
var historiesToRemove = new List<WorkHistory>();
foreach (var history in this.WorkHistories)
{
if (history.Tool.Equals(item))
{
historiesToRemove.Add(history);
}
}
While this piece of code produces an empty list:
var historiesToRemove = this.WorkHistories.Where(history => history.Tool.Equals(item)).ToList();
Any ideas why could this happen?
REASON:
I didn’t properly implement IDbSet’s IQueryable properties. This made my LINQ act wrong.
IDbSetthat you mention in a comment is part of the entity framework so your 2 pieces of code are not equivalent. The LINQ is an expression tree that gets converted to SQL by EF whereas the first bit of code retreieves the entire table from the database and executes the loop in memory. Profile the database to find out what SQL is executing in the database and this should give you an idea why the linq does not do what you want.