score:1
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();
Source: stackoverflow.com
Related Query
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Linq to SQL: WHERE IN statement
- Linq Query with a Where clause in an Include statement
- Why no intellisense when LINQ statement has no where clause?
- LINQ WHERE method alters source collection
- Error within Where statement in LINQ
- Where can I view LINQ source code?
- Linq WHERE EF.Functions.Like - Why direct properties work and reflection does not?
- return from a linq where statement
- LINQ Source Code Available
- Use == operator with generic type in a Where Linq statement
- Linq with where clause in many-to-many EF Code First object
- LINQ Where statement in collection
- When Where clause is used inside Linq statement produces different results than when used outside
- Linq statement with two where clauses
- LINQ - CASE statement in WHERE clause
- Entity Framework - LINQ statement with WHERE clause based on NULLABLE parameter
- C# - Linq optimize code with List and Where clause
- In a LINQ statement using closures, is it possible to specify a where clause within a closure?
- creating Linq to sqlite dbml from DbLinq source code
- Where does this Linq statement get the selected value from?
- How to create regex for mathing linq statement NOT contains where clause?
- Why do casting to (int?) work while (string?) doesnt in LINQ queries
- C# - Linq statement where skip last X rows
- LINQ Statement with where clause makes the execution slow
- LINQ statement where result count is used in expression's condition
- How to place an If/Else statement in a where clause in a linq query?
- How to use OR operator in LINQ WHERE statement
- WHERE statement does not work in unit test using It.IsAny
- Better way to write Linq Where statement
More Query from same tag
- How do we switch from Telerik Open Access to anything else?
- Get a random item from a collection that does not already exist in another collection - LINQ?
- How can I find out if a strongly typed table contains a value from another strongly typed table in C# using Linq
- Query expressions vs Lambda expressions
- Nhibernate 3.0 LINQ: Problem returning to IQueryable (non generic version) - doesn't allow ToList()
- Appending to Query if not null
- LINQ to Entities does not recognize the method 'Double Parse(System.String)' cannot be translated into a store expression C# asp.net
- How do I query many-to-many relationship in NHibernate
- How to get a count of every single element in an xml file using linq to xml?
- Entity Framework Linq embed lambda expressions into a reusable function
- Comparing and allocating elements in Linq
- Can't get access to a model element that is connected with a Left Outer Join
- How can I populate data inside a LINQ .Select from the values of each character of a string?
- c# flatten nested object
- Can't figure out the Logic and Syntax to create this list
- How can i Select many with contains in LINQ?
- Faster SQL Inserts?
- Multiple Column Check in Linq
- Return async task using LINQ select
- Using LINQ and lambdas to search Dictionary>Class>List>Struct data
- LINQ query with Distinct and WHERE clause
- Left outer join IEnumerable<T> to DataTable - parameter cannot be null error
- Can someone explain why these two linq queries return different results?
- Linq Date Performance Death
- Change this Loop by LINQ?
- How can I use linq to remove a certain string from an array while it exists as the first or last element
- Change the type of one of the objects property and sort it using LINQ
- How to group by the records to a list of category items
- Compare two arrays in LINQ query
- Is LinqToSQL the same as Linq?