簡體   English   中英

如何在C#中創建此表達式樹(動態lambda)?

[英]How do I create this expression tree (dynamic lambda) in C#?

我試圖創建一個表示以下內容的表達式樹(動態linq):有我的自定義類和集合。

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

這是我的課程:

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;

謝謝...

您是否正在嘗試將表達式樹合並為一個委托? 如果是這樣,下面是一個示例:

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();

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM