我有两个表的数据库:大厦LINQ的一般查询
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;
}
什么是正确的方法来做到这一点?
我试着使用表达式树,但我得到了他们的stucked ...
如何与两个点建立体现在哪里? (t.t2.prop == TRUE)
如何传递匿名类型(其加入()生成),以通用
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);
[This](http://stackoverflow.com/questions/307512/how-do-i-apply-orderby-on-an-iqueryable-using-a-string-column-name-within-a-gene )用于'OrderBy',但可以作为一个起点。 –
你需要属性参数是一个字符串吗?它可以是一个表达式吗? – Magnus
它可以表达式 – Michael