Skip to content

Instantly share code, notes, and snippets.

@akimboyko
Created December 11, 2012 15:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akimboyko/4259463 to your computer and use it in GitHub Desktop.
Save akimboyko/4259463 to your computer and use it in GitHub Desktop.
Dynamic wher clause: System.Linq.Dynamic and PredicateBuilder
void Main()
{
// add assembly System.Linq.Dynamic and using System.Linq.Dynamic
var fruits = new [] { new Product { Id = 1, ColA = "Apple", ColB = "Red" }, new Product { Id = 2, ColA = "IceCream", ColB = "White" } };
var cars = new [] { new Product { Id = 1, ColA = "Ferrary", ColB = "Red" }, new Product { Id = 2, ColA = "Ford", ColB = "Black" } };
var predicate =
PredicateBuilder
.True<Tuple<Product, Product>>()
.And(t => t.Item1.ColA != t.Item2.ColA)
.And(t => t.Item1.ColB == t.Item2.ColB)
.Compile();
(from fruit in fruits
join car in cars on fruit.Id equals car.Id
select Tuple.Create(fruit, car))
.Where(predicate)
.Dump();
(from fruit in fruits
join car in cars on fruit.Id equals car.Id
select new { fruit, car })
.AsQueryable()
.Where("fruit.ColA != car.ColA")
.Where("fruit.ColB == car.ColB")
.Dump();
}
class Product
{
public int Id;
public string ColA;
public string ColB;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment