构建Linq通用查询

3

我有一个包含两个表的数据库:

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;
}

如何正确地做这件事?

我尝试使用表达式树,但遇到了困难...

  1. How to build expression with two dots? (t.t2.prop == true)

  2. 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);
    

1
这是关于OrderBy的内容,但可以作为一个起点。 - L.B
你需要将属性参数设置为字符串吗?它可以是一个表达式吗? - Magnus
1个回答

2

将条件作为Join方法源代码的一部分,怎么样?

采用这种方法,您的条件需要Expression<Func<B,true>>,您可以轻松地使用表达式树来创建一个。

List<T> result;

var param = Expression.Parameter(typeof(B), "x");
var trueExp = Expression.Constant(true);
var condition = Expression.Equal(Expression.Property(param, prop), trueExp);
var whereLambda = Expression.Lambda<Func<B, bool>>(condition, param);

using (var db = new DataContext())
{
    result = db.Table1
               .Join(db.Table2.Where(whereLambda),
                     t1 => t1.Id,
                     t2 => t2.Id,
                     (t1, t2) => new { t1, t2 })
               .Select(t => t.t1)
               .ToList();
}
return result;

更新

如果您想按照原始设计进行操作,则应该使编译器自动推断匿名类型:

public static Expression<Func<T, bool>> GetPropertyCondition<T>(T source, string prop)
{
    var param = Expression.Parameter(typeof(T), "x");
    var trueExp = Expression.Constant(true);
    var condition = Expression.Equal(
                        Expression.Property(
                            Expression.Property(param, "t2"), prop),
                            trueExp);
    var whereLambda = Expression.Lambda<Func<T, bool>>(condition, param);
    return whereLambda;
}

你可以这样调用:

var result = new List<A>();

var anonymous = new { t1 = (A)null, t2 = (B)null };
var condition = GetPropertyCondition(anonymous, prop);

using (var db = new DataContext())
{
    result = db.Table1.AsQueryable()
               .Join(db.Table2.AsQueryable(), t1 => t1.Id, t2 => t2.Id, (t1, t2) => new { t1, t2 })
               .Where(condition)
               .Select(t => t.t1)
               .ToList();
}
return result;

它利用了这样一个事实,即程序集中具有相同属性集合(包括属性名称和属性类型)的每个匿名类型对象都共享相同的基础匿名类。因此,Join 扩展方法返回的类型与 typeof(anonymous) 匹配。


谢谢您的回复。我明天会在工作中尝试这个)) - Michael

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接