Filtering lists using LINQ

Have a look at the Except method, which you use like this: var resultingList = listOfOriginalItems.Except(listOfItemsToLeaveOut, equalityComparer) You’ll want to use the overload I’ve linked to, which lets you specify a custom IEqualityComparer. That way you can specify how items match based on your composite key. (If you’ve already overridden Equals, though, you shouldn’t need … Read more

linq to entities vs linq to objects – are they the same?

That is definitely not the case. LINQ-to-Objects is a set of extension methods on IEnumerable<T> that allow you to perform in-memory query operations on arbitrary sequences of objects. The methods accept simple delegates when necessary. LINQ-to-Entities is a LINQ provider that has a set of extension methods on IQueryable<T>. The methods build up an expression … Read more

LINQ – is SkipWhile broken?

It’s not broken. SkipWhile will only skip items in the beginning of the IEnumerable<T>. Once that condition isn’t met it will happily take the rest of the elements. Other elements that later match it down the road won’t be skipped. int[] sequence = { 3, 3, 1, 1, 2, 3 }; var result = sequence.SkipWhile(i … Read more

LINQ return items in a List that matches any Names (string) in another list

var products = shopProducts.Where(p => listOfProducts.Any(l => p.Name == l.Name)) .ToList(); For LINQ-to-Objects, if listOfProducts contains many items then you might get better performance if you create a HashSet<T> containing all the required names and then use that in your query. HashSet<T> has O(1) lookup performance compared to O(n) for an arbitrary IEnumerable<T>. var names … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)