var parameter = Expression.Parameter(typeof(A), "a"); var member = Expression.Field(parameter, "b"); member = Expression.Field(member, "id"); var constant = Expression.Constant(9); var bin = Expression.GreaterThan(member, constant); var lambda = Expression.Lambda<Func<A, bool>>(bin, parameter); h.Where(lambda.Compile()).ToList();
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Test { public class A { public int Id; public string Name; public B b; } public class B { public int Id; } public partial class Form1 : Form { public Form1() { InitializeComponent(); } List<A> h = new List<A>(){ new A(){ b = new B(){ Id = 1}}, new A(){ b = new B(){ Id = 3}}, new A(){ b = new B(){ Id = 11}}, new A(){ b = new B(){ Id = 13}}, new A(){ b = new B(){ Id = 12}}, new A(){ b = new B(){ Id = 7}}, new A(){ b = new B(){ Id = 9}} }; private void Form1_Load(object sender, EventArgs e) { var parameter = Expression.Parameter(typeof(A), "a"); var member = Expression.Field(parameter, "b"); member = Expression.Field(member, "id"); var constant = Expression.Constant(9); var bin = Expression.GreaterThan(member, constant); var lambda = Expression.Lambda<Func<A, bool>>(bin, parameter); h.Where(lambda.Compile()).ToList().ForEach(n => MessageBox.Show(n.b.Id.ToString())); } } }
感谢 博客¥ 回答,谢谢,问题解决了