c# - 构建 Linq 通用查询

标签 c# .net database linq expression-trees

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

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. 如何用两个点构建表达式? (t.t2.prop == true)

  2. 如何将匿名类型(由 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);
    

最佳答案

如何将条件作为 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;

它使用了这样一个事实,即程序集中每个具有相同属性集(属性名称和属性类型都必须匹配)的匿名类型对象共享相同的底层匿名类。所以typeof(anonymous)这里匹配 Join 返回的类型扩展方法。

关于c# - 构建 Linq 通用查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21077888/

相关文章:

c# - 向 DataTable 添加空行

.net - 电源外壳;即使没有 IPv6,为什么此代码总是返回 'True'?这是错的吗?

sql - 存储人类可读的 UUID 的最小方法是什么?

c# - 如何实现A*算法?

c# - 如何为所有 IEnumerable<Int32> 覆盖 ToString() 方法?

c# - 创建新线程时,不对 GUI 进行更改 (C#)

php - 使用 PHP 单击提交按钮后无法将数据保存到数据库中

Facebook 粉丝页面上的 PHP - 这可能吗?

c# - 序列化循环引用对象的最佳方法是什么?

javascript - C# 中的作用域、变量访问