Linq to objects Predicate Builder

Just change PredicateBuilder to use delegates instead of expression trees and use lambdas to build the results: public static class DelegatePredicateBuilder { public static Func<T, bool> True<T>() { return f => true; } public static Func<T, bool> False<T>() { return f => false; } public static Func<T, bool> Or<T>(this Func<T, bool> expr1, Func<T, bool> expr2) … Read more

Lambda Expression for join

The lambda for a Join is a bit involved – here’s a simple example: List<Person> People = new List<Person>(); List<PersonType> PeopleTypes = new List<PersonType>(); var joined = People.Join(PeopleTypes, PeopleKey => PeopleKey.PersonType, PeopleTypesKey => PeopleTypesKey.TypeID, (Person, PersoneType) => new { Name = Person.Name, TypeID = PersoneType.TypeID }); I usually find the query syntax a lot more … Read more

Linq OrderBy against specific values

If you put your preferences into a list, it might become easier. List<String> data = new List<String> { “A”,”B”,”A”,”C”,”B”,”C”,”D”,”E” }; List<String> preferences = new List<String> { “A”,”B”,”C” }; IEnumerable<String> orderedData = data.OrderBy( item => preferences.IndexOf(item)); This will put all items not appearing in preferences in front because IndexOf() returns -1. An ad hoc work around … 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

Code equivalent to the ‘let’ keyword in chained LINQ extension method calls

Let doesn’t have its own operation; it piggy-backs off of Select. You can see this if you use “reflector” to pull apart an existing dll. it will be something like: var result = names .Select(animalName => new { nameLength = animalName.Length, animalName}) .Where(x=>x.nameLength > 3) .OrderBy(x=>x.nameLength) .Select(x=>x.animalName);

LINQ identity function

Unless I misunderstand the question, the following seems to work fine for me in C# 4: public static class Defines { public static T Identity<T>(T pValue) { return pValue; } … You can then do the following in your example: var result = enumerableOfEnumerables .SelectMany(Defines.Identity); As well as use Defines.Identity anywhere you would use a … Read more

C# Linq where clause as a variable

You need to assembly an Expression<Func<T, bool>> and pass it to the Where() extension method: Expression<Func<T, bool>> whereClause = a => a.zip == 23456; var x = frSomeList.Where(whereClause); EDIT: If you’re using LINQ to Objects, remove the word Expression to create an ordinary delegate.

Linq to Objects – return pairs of numbers from list of numbers

None of the default linq methods can do this lazily and with a single scan. Zipping the sequence with itself does 2 scans and grouping is not entirely lazy. Your best bet is to implement it directly: public static IEnumerable<T[]> Partition<T>(this IEnumerable<T> sequence, int partitionSize) { Contract.Requires(sequence != null) Contract.Requires(partitionSize > 0) var buffer = … Read more