How does deferred LINQ query execution actually work?

Your query can be written like this in method syntax: var query = numbers.Where(value => value >= threshold); Or: Func<int, bool> predicate = delegate(value) { return value >= threshold; } IEnumerable<int> query = numbers.Where(predicate); These pieces of code (including your own query in query syntax) are all equivalent. When you unroll the query like that, … 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

Greater Than Condition in Linq Join

You can’t do that with a LINQ joins – LINQ only supports equijoins. However, you can do this: var query = from e in entity.M_Employee from p in entity.M_Position where e.PostionId >= p.PositionId select p; Or a slightly alternative but equivalent approach: var query = entity.M_Employee .SelectMany(e => entity.M_Position .Where(p => e.PostionId >= p.PositionId));

CSV to object model mapping

For the specific data shown in your question… var yourData = File.ReadAllLines(“yourFile.csv”) .Skip(1) .Select(x => x.Split(‘,’)) .Select(x => new { Plant = x[0], Material = x[1], Density = double.Parse(x[2]), StorageLocation = int.Parse(x[3]) }); If you already have a type declared for your data then you can use that rather than the anonymous type. Note that … Read more

How to implement left join in JOIN Extension method

Normally left joins in LINQ are modelled with group joins, sometimes in conjunction with DefaultIfEmpty and SelectMany: var leftJoin = p.Person.Where(n => n.FirstName.Contains(“a”)) .GroupJoin(p.PersonInfo, n => n.PersonId, m => m.PersonId, (n, ms) => new { n, ms = ms.DefaultIfEmpty() }) .SelectMany(z => z.ms.Select(m => new { n = z.n, m })); That will give a … Read more

LINQ performance Count vs Where and Count

The crucial thing is in the implementation of Where() where it casts the IEnumerable to a List<T> if it can. Note the cast where WhereListIterator is constructed (this is from .Net source code obtained via reflection): public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) { if (source is List<TSource>) return new WhereListIterator<TSource>((List<TSource>)source, predicate); return … Read more

How does PredicateBuilder work

Let’s say you have: Expression<Func<Person, bool>> isAdult = p1 => p1.Age >= 18; // I’ve given the parameter a different name to allow you to differentiate. Expression<Func<Person, bool>> isMale = p2 => p2.Gender == “Male”; And then combine them with PredicateBuilder var isAdultMale = isAdult.And(isMale); What PredicateBuilder produces is an expression that looks like this: … Read more

Can I use Linq’s Except() with a lambda expression comparer?

For any one still looking; here’s another way of implementing a custom lambda comparer. public class LambdaComparer<T> : IEqualityComparer<T> { private readonly Func<T, T, bool> _expression; public LambdaComparer(Func<T, T, bool> lambda) { _expression = lambda; } public bool Equals(T x, T y) { return _expression(x, y); } public int GetHashCode(T obj) { /* If you … Read more