简体   繁体   中英

How to set Join for IQueryable<T> linq expression

I have done dynamic linq expression like below

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? How to achieve 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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