linq where AND with multiple OR's

innov83r

I have a DataTable that I'm trying to find distinct rows that differ on a set of columns using LINQ. The DataTable has the following column structure: name, department, shift, empType, onCall. I'm trying to find out all rows that have the same name and department, but where shift, empType or onCall could differ. I've tried the following LINQ but it just gives me the duplicates that are present.

from DataRowView e in empDataTable
group e by new
{
  Name = e.Row["name"],
  Dept = e.Row["department"],
  Shift = e.Row["shift"],
  EmployeeType = e.Row["empType"],
  OnCall = e.Row["onCall"]
}
into g
where (g.Count() > 1)
select g;

Any help would be appreciated.

Sergey Kalinichenko

You need to exclude the columns that you want to allow to be different from the GROUP BY key, otherwise LINQ would create a separate group for each combination, so the count would remain 1 for rows other than full duplicates (i.e. rows with all five columns matching):

from DataRowView e in empDataTable
group e by new
{
  Name = e.Row["name"],
  Dept = e.Row["department"]
}
into g
where (g.GroupBy(g => new {
        Shift = e.Row["shift"],
        EmployeeType = e.Row["empType"],
        OnCall = e.Row["onCall"]
    }).Count() > 1)
select g;

The inner GROUP BY groups by the remaining three columns. If all three columns are the same for a {name, department} pair, the group is ignored. Otherwise, the group is reported as a query result.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事