如何设置Join for IQueryable <t> linq表达式

时间:2017-08-07 13:23:17

标签: c# linq

我已完成动态linq表达式,如下所示

private static List<Employee> FilterData(
    IQueryable<system> systemData, 
    IQueryable<employee> employeeData)
{
    var filterData = systemData.Join(
        employeeData,
        s => s.id, em => em.system_id, 
        (systemEntity, employeeEntity) => new { employee = employeeEntity});
}
  

在Join中使用Generic时如何进行以下连接?   如何实现s =&gt; s.id,em =&gt; em.system_id

private static List<TEntity> FilterData(
    IQueryable<T> systemData, 
    IQueryable<TEntity> employeeData)
{
    //var filterData = systemData.Join(
    //    employeeData,
    //    s => s.id, em => em.system_id, 
    //    (systemEntity, employeeEntity) => new { employee = employeeEntity});
}

1 个答案:

答案 0 :(得分:2)

您可以为方法添加通用约束:

public interface ISystem
{
  int id { get; }
  ...
}

public interface IEmployee
{
  ...
  int system_id { get; }
  ...
}

private static List<TEmployee> FilterData<TSystem, TEmployee>(
  IQueryable<TSystem> systemData, IQueryable<TEmployee> employeeData)
  where TSystem : ISystem
  where TEmployee: IEmployee
{
  var filterData = 
      systemData.Join(employeeData,
                      s => s.id, 
                      em => em.system_id, 
                      (systemEntity, employeeEntity) => new { employee = employeeEntity});
}

请注意,您似乎只对使用现有system_id的员工感兴趣,如果是这种情况,加入可能是过度的(如果您从系统中提取属性则不会)