【发布时间】:2014-01-12 17:30:35
【问题描述】:
我有一个包含两个表的数据库:
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;
}
这样做的正确方法是什么?
我尝试使用表达式树,但我被他们卡住了......
如何用两个点构建表达式? (t.t2.prop == true)
-
如何将匿名类型(由 Join() 生成)传递给泛型
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 适用于
OrderBy,但可以作为起点。 -
是否需要属性参数为字符串?可以改为表达式吗?
标签: c# .net database linq expression-trees