简体   繁体   English

如何为IQueryable设置Join <T> LINQ表达

[英]How to set Join for IQueryable<T> linq expression

I have done dynamic linq expression like below 我已经完成了如下的动态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});
}

How to do below join when I use Generic in Join? 当我在Join中使用Generic时,下面的join如何做? How to achieve s => s.id, em => em.system_id 如何实现s => s.id,em => 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});
}

You can add generic constraints to your method: 您可以在方法中添加通用约束:

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

note that it seems like you're only interested in getting the employees with an existing system_id, if that's the case, a Join is probably overkill (it would not if you extracted properties from the system) 请注意,您似乎只想让雇员拥有一个现有的system_id,如果这样的话,Join可能会过大(如果您从系统中提取属性则不会)

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

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