如何在C#中创建此表达式树(动态lambda)?
我正在尝试创建一个表达式树(dynamiclinq),它表示以下内容:有我的自定义类和集合如何在C#中创建此表达式树(动态lambda)?,c#,linq,dynamic-linq,C#,Linq,Dynamic Linq,我正在尝试创建一个表达式树(dynamiclinq),它表示以下内容:有我的自定义类和集合 List<Contract> sel = contractList.Where(s => s.account.Age > 3 && s.productList.Any(a => a.ProductType == "abc")).ToList(); List sel=contractList.Where(s=>s.account.Age>3&&s.produc
List<Contract> sel = contractList.Where(s => s.account.Age > 3 && s.productList.Any(a => a.ProductType == "abc")).ToList();
List 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;
公共类帐户
{
公共整数{get;set;}
公共十进制余额{get;set;}
公共账户(整数,十进制余额)
{
这个。年龄=年龄;
平衡=平衡;
}
}
公共类产品
{
公共字符串ProductType{get;set;}
公共产品(字符串ProductType)
{
this.ProductType=ProductType;
}
}
公共类合同
{
公共int ID{get;set;}
公共帐户{get;set;}
公共列表productList{get;set;}
公共合同(int ID、帐户、产品、列表productList)
{
this.ID=ID;
这个账户=账户;
this.productList=productList;
}
}
公开名单;
谢谢…您是否正在尝试将表达式树合并到单个委托中?如果是,下面是一个示例:
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();
三个以上的公共静态布尔隔离器,并具有ABC产品类型(合同)
{
如果(合同、账户、账龄>3
&&contract.productList.Any(a=>a.ProductType==“abc”))
{
返回true;
}
返回false;
}
List sel=contractList.Where(IsolderThree和HasAbcProductType).ToList();
希望这有帮助 最好的办法是编译一个你想要的例子,然后查看reflector或ildasm;在您的例子中有点复杂,因为您给出的示例不涉及表达式树-它通过
Enumerable
将LINQ用于对象,因此它是匿名方法的委托;不是表情tree@svick我没有试过,因为对我来说太复杂了,我无法解决,我不知道从哪里开始。“如果你给我任何提示,我可以做它。”MarcGravel谢谢,但我怎么能看反射器呢?我以前试过。