我有兩個表的數據庫:大廈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