1

2 つのデータ オブジェクトがあります。従業員とマネージャー

class Employee{
int EmpId;
int EmpName }

Class Manager{
int ManagerID; //is the empId of an employee who is Manager
}

単一のステートメントで LINQ を使用するマネージャーではない EmpName のリストが必要です。

私はこれを試しました:

var employeeIdsWhoAreNotManagers = EmployeeList.Select(x=>x.EmpId).Except(Managerlist.Select(x=>x.ManagerId));

しかし、これは EmpId のみを返します。次に、 EmpName を取得するためにもう 1 つ linq を作成する必要があります。

アップデート1:

 empNamesList = EmployeeList.Where(x=>x.employeeIdsWhoAreNotManagers.Contains(x.empId)).Select(x=>x.empName);

EmpName リストを直接返す単一の LINQ クエリに結合するにはどうすればよいですか?

4

1 に答える 1

6

オブジェクトへのLinqなら拡張メソッドが使えるExceptBy

public static IEnumerable<T1> ExceptBy<T1, T2, R>(
    this IEnumerable<T1> source, 
    IEnumerable<T2> other, 
    Func<T1,R> keySelector1, 
    Func<T2,R> keySelector2)
{
    HashSet<R> set = new HashSet<R>(other.Select(x => keySelector2(x)));

    return source.Where(item => set.Add(keySelector1(item)));
}

.

 EmployeeList.ExceptBy(Managerlist, x=>x.EmpId , y=>y.ManagerId));
于 2013-09-25T16:27:05.587 に答える