I have a database with two tables:
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; }
}
I want to write function, which will take as parameter "Prop1", "Prop2", ... , "PropX" and return appropriate rows from Table1. Something like this:
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;
}
What is a proper way to do this?
I've tried to use Expression Tree, but I got stucked their...
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);