0

I am trying to create an expression tree (dynamic linq) that represents the following: There are my custom classes and collections.

List<Contract> sel = contractList.Where(s => s.account.Age > 3 && s.productList.Any(a => a.ProductType == "abc")).ToList();

Here my classes:

public class Account
{
    public int Age { get; set; }
    public decimal Balance { get; set; }

    public Account(int Age, decimal Balance)
    {
        this.Age = Age;
        this.Balance = Balance;
    }
}

public class Product
{
    public string ProductType { get; set; }

    public Product(string ProductType)
    {
        this.ProductType = ProductType;
    }
}

public class Contract
{
    public int ID { get; set; }
    public Account account { get; set; }
    public List<Product> productList { get; set; }
    public Contract(int ID, Account account, Product product, List<Product> productList)
    {
        this.ID = ID;
        this.account = account;
        this.productList = productList;
    }
}

public List<Contract> contractList;

Thank you...

Jose
  • 11
  • 2
  • 3
    The best thing to do is to compile an example of what you want, and look in reflector or ildasm; it is a *bit* complicated in your case, because the example you give ***doesn't involve an expression tree*** - it is using LINQ-to-Objects via `Enumerable`, so it is a delegate to an anonymous method; *not* an expression tree – Marc Gravell Feb 05 '13 at 14:26
  • @svick i havent tried because for me its so complicated, i couldn't solve and i don't know where to start. If you give me any tips, i can work on it. – Jose Feb 05 '13 at 15:03
  • @MarcGravell thanks but how can i look in reflector? i vent tried before. – Jose Feb 05 '13 at 15:04

1 Answers1

0

Are you trying to consolidate your expression tree into a single delegate? If so, here is an example:

public static bool IsOlderThanThreeAndHasAbcProductType(Contract contract)
{
    if (contract.account.Age > 3
        && contract.productList.Any(a => a.ProductType == "abc"))
    {
        return true;
    }
    return false;
}

List<Contract> sel = contractList.Where(IsOlderThanThreeAndHasAbcProductType).ToList();

Hope this helps!

Aaron Hawkins
  • 2,611
  • 1
  • 20
  • 24