我有一个包含两个表的数据库:
public class A {
public string Name { get; set; }
public int Id { get; set; }
}
public class B {
public int Id { get; set; }
public bool Prop1 { get; set; }
public bool Prop2 { get; set; }
public bool Prop3 { get; set; }
public bool Prop4 { get; set; }
}
public class DataContext : DbContext {
DbSet<A> Table1 { get; set: }
DbSet<B> Table2 { get; set; }
}
我想编写一个函数,它将以参数 "Prop1"、"Prop2"、...、"PropX" 的形式接收,并从 Table1 返回相应的行。 类似于以下内容:
public List<A> GetByProp(string prop) {
var result = new List<A>();
using (var db = new DataContext()) {
result = db.Table1.Join(db.Table2, t1=>t1.Id, t2=>t2.Id, (t1,t2)=>new {t1,t2}).
Where(????????). //t=>t.t2.prop == true
Select(t=>t.t2);
}
return result;
}
如何正确地做这件事?
我尝试使用表达式树,但遇到了困难...
How to build expression with two dots? (t.t2.prop == true)
How can I pass anonymous type (which Join() generates) to generic
var p = Expression.Parameter(typeof(???), t2); //??? - anonymous class var t = Expression.Constant(true, typeof(bool)); var e = Expression.Equal(p, t); var l = Expression.Lambda<Func<???, bool>>(e, p);
OrderBy
的内容,但可以作为一个起点。 - L.B