【问题标题】:Runtime joins to query within Entity Framework运行时连接到实体框架内的查询
【发布时间】:2016-01-14 18:46:23
【问题描述】:

我有一个使用实体框架映射的数据库。 Entity Framework 正在以类似的方式生成数据库对象的 C# 代码,如下所示。为简单起见,我创建了Parent、Child、GrandChild 层次结构,但实际的数据库包含更长的层次结构和许多其他字段。

class Parent
{
  string name;
  int id;
  datetime DateOfBirth;
}

class Child
{
    string name;
    int id;
    int ParentId; ( FK reference to Parent Child )
}

class GrandChild
{
    string name;
    int id;
    int ChildId; (FK reference to Child )
}

现在,我正在构建一个将在运行时提供过滤器的 api。我的意思是,有些查询可能是

  1. 为 ParentId =1 提供所有 GrandChild 行
  2. 为 ChildName = "x" 提供所有 Grandchild 行
  3. 为 DateOfbirth = "x/y/z" 的 Parent 提供所有 GrandChild 行

那么,我如何在 C# 代码中构建,使用 LINQ 或表达式树来动态/运行时创建谓词和连接过滤器。

以下网址: https://msdn.microsoft.com/library/bb882637.aspx

展示了如何创建动态查询,但没有展示如何 INNER JOIN 多个此类查询。有人知道怎么做吗?

这个 Stackoverflow 答案: The parameter '***' was not bound in the specified LINQ to Entities query expression

还强调了如何动态创建过滤器,而不是如何加入它们。

有谁知道如何创建动态查询来过滤行并加入查询?如果您需要更多信息,请与我们联系。谢谢

【问题讨论】:

  • 使用EF,你可能不需要Join,概念模型会有导航属性,可以直接访问。 coding.abel.nu/2012/06/dont-use-linqs-join-navigate
  • 您的真实模型是否也有id 和name 值?或者这只是一个例子,你的真实模型都有不同的属性等和很多属性?
  • @Agal 感谢您的链接。知道如何动态创建此类导航 linq 查询。
  • @AlexanderDerck 真实模型具有 id、name 以及调用者可能想要过滤的许多其他属性
  • 如果模型的属性是一致的,你可以写一些带有接口约束的通用扩展方法,但是如果模型都具有随机属性等,那就不太可能了。

标签: c# entity-framework linq linq-to-sql expression-trees


【解决方案1】:

好吧,我假设这不是您的真实模型,因为您应该有属性而不是字段,并且您的实体必须是 public。

如果您使用navigation properties 表示您的数据库关系,您可以像这样创建extension method:

static IQueryable<TEntity> Select<TEntity>(this  IQueryable<TEntity> query, List<Expression<Func<TEntity, bool>>> filters = null,
                                           List<Expression<Func<TEntity, object>>> includes = null)
{
   if (includes != null)
   {
      query = includes.Aggregate(query, (current, include) => current.Include(include));
   }
   if (filters != null)
   {
      query = filters.Aggregate(query, (current, filter) => current.Where(filter)); //at the end this is going to be translated to condition1 && condition2 ...
   }
   return query;
}

在第一个列表中,您传递了要应用于查询的所有条件,第二个列表帮助您加载查询中需要的相关实体:

var conditions = new List<Expression<Func<GrandChild, bool>>>() { (t) => t.Child.Parent.ParentId==1 };
var includes = new List<Expression<Func<GrandChild, object>>>() { (t) => t.Child.Parent };

var query= yourContext.GrandChilds.Select(filters,includes);

【讨论】:

  • 我认为 OP 可能会问如何动态构建var conditions。 AFAIK,这在 EF6 中不受支持,但我认为 EF7 可能已经讨论过这样做。
【解决方案2】:

1 .为 ParentId =1 提供所有 GrandChild 行

 var grandChildData=from grand in GrandChild
                    join ch in Child on grand.ChildId equals ch.Id
                    where ch.ParentId==1
                    select grand;

【讨论】:

    【解决方案3】:
    1. 为 ChildName = "x" 提供所有孙行

       var grandChildData=from grand in GrandChild
                     join ch in Child on grand.ChildId equals ch.Id
                     where ch.name = "x"
                     select grand;
      
    2. 为 DateOfbirth = "x/y/z" 的 Parent 提供所有 GrandChild 行

       var grandChildData=from grand in GrandChild
                         join ch in Child on grand.ChildId equals ch.Id
                         join p in Parent on ch.ParentId equals p.id
                         where p.DateOfbirth==Convert.ToDateTime("2016/01/01")
                         select grand;
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 2012-07-07
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多