简体   繁体   English

如何动态编写此linq查询C#

[英]How to write this linq query dynamically c#

I am trying to write a dynamic query using Expression Tree . 我正在尝试使用Expression Tree编写动态查询。 We have following entities in our schema: 我们的架构中包含以下实体:

  • Tenant 承租人
  • Employee 雇员
  • Services 服务

There is a 1 to many relationship between Tenant and Serivces and Many to Many relationship between Employee and Serivces. 有租户和Serivces和很多之间和员工之间Serivces 许多关系的1对多的关系。 Both Tenant and Employee contains the ICollection of Services. 租户和员工都包含ICollection of Services。

Now I have to write the following query dynamically: 现在,我必须动态编写以下查询:

  context.{EntityDbSetResolveAtRunTime out of Tenant OR Employee entity}
         .SingleOrDefault(p => 
          p.{PropertyResolveAtRunTime out of TenantId or EmployeeId} == id)
         .Services;

Below is the code i tried yet is: 以下是我尝试过的代码:

public class ServiceRpository<TEntity> : where TEntity : class
{
    private DbSet<TEntity> dbset;

    public ServiceRepository(MyContext context)
    {
      dbset = context.Set<TEntity>();
    }

    public List<Services> Get(string id)  
    {
       //p 
       ParameterExpression predicateVariable = Expression.Parameter(typeof(TEntity), "p");
       //p => p.TenantId OR p => p.EmployeeId
       Expression left = Expression
               .Property(predicateVariable, typeof(TEntity)
               .GetProperty(typeof(TEntity).Name + "Id"));
       //id
       Expression rightConstant = Expression.Constant(id);
       //p => p.TenantId == id OR p => p.EmployeeId == id
       Expression predicateBody = Expression.Equal(left, rightConstant);

       // predicateBody variable contains 
       // p => p.TenantId == id OR p => p.EmployeeId == id

       //The below code will give us the Tenant Or Employee entity
       var entity = dbset.SingleOrDefault(Expression.Lambda<Func<TEntity, bool>>(
           predicateBody, new ParameterExpression[] { predicateVariable }));


       // Now the problem is how i get Serivces out of the entity, something like
       // entity.Services;
       // and what should be 
       // the return type of this function which orignally i want List<Services>

    }
} 

Please check the comments in the code. 请检查代码中的注释。

Thanks! 谢谢!

I have no way to test it right now, but following should work (or at least guide you into right direction). 我目前无法对其进行测试,但是下面的步骤应该可以工作(或者至少可以指导您朝正确的方向发展)。

Add Include call to fetch services: 添加Include调用以获取服务:

//The below code will give us the Tenant Or Employee entity
var entity = DbSet.Include("Services").SingleOrDefault(
    Expression.Lambda<Func<TEntity, bool>>(predicateBody, new ParameterExpression[] { predicateVariable }));

Get your Services property value as IEnumerable<Service> and call ToList() to make sure List<Service> is returned: 获取您的Services属性值为IEnumerable<Service>然后调用ToList()以确保返回List<Service>

return ((IEnumerable<Service>)typeof(TEntity).GetProperty("Services").GetValue(entity, null)).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM