score:1

Accepted answer

idbset that 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.

score:1

edit: just saw you have tolist on the end of the query, so the below does not apply.


read up on closures and linq. probably 'item' is changing before you run the query.

example problem code:

var filter = "compare";

var query = from m in typeof(string).getmethods()
            where m.name.contains(filter)
            select new { m.name, parametercount = m.getparameters().length };

filter = "indexof";

foreach (var item in query)
    console.writeline(item);

score:1

this is just a guess, but i would say that you've probably provided a definition of equality that is different than that used by the linq translation in ef. in ef i believe that it uses property equality, whereas you might only be checking that the ids are the same. i suggest that you explicitly encode the definition of equality that you want to check in your linq statement. the reason that your equality definition works in the first case is that enumerating the idbset brings it into memory and thus invokes your version of equals rather than the linq-to-ef translation of equals.

 var historiestoremove = this.workhistories.where( h => h.tool.id == tool.id )
                                           .tolist();

Related Query

More Query from same tag