Created
December 11, 2012 15:45
-
-
Save akimboyko/4259463 to your computer and use it in GitHub Desktop.
Dynamic wher clause: System.Linq.Dynamic and PredicateBuilder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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