使用你现在得到的结果,而不是检查是否有更多项目(skip1-any),你可以检查是否有任何项目不等于第一个项目的data3。
借助使用第一个 data3 分配变量,这可以在查询语法中更容易完成:
var lDuplicates = from x in lstObjects
group x by new { x.data1, x.data2} into g //g now contains groups with unique data1 and data3 objects
let first = g.First().data3 //assign the first data3 to an intermediate variable
where g.Skip(1).Any(x=>x.data3 != first) //check if there are any entries that have an deviating data3
select g;
上面选择了与条件相对应的所有组(如果需要,可以在同一个查询中展平)。
但这也意味着一个组可以包含 2 个“DE”,只要至少没有一个“DE”。不确定这是否是要求。要唯一地获取所有对象(展平):
var lDuplicates = from x in lstObjects
group x by new { x.data1, x.data2} into g //g now contains groups with unique data1 and data3 objects
let d3 = g.Select(x=>x.data3).Distinct().ToList() //a list of unique data3 properties
where d3.Count() > 1 //only with more than one unique data3
from data3 in d3
select new{g.Key.data1,g.Key.data2, data3}; //create a new object
注意,上面创建了一个 new 对象,因为对于多个匹配项,使用哪个对象? (它可以具有比 data1、data2 和 data3 更多的属性)。选择每个“data3”组的第一个对象:
var lDuplicates = from x in lstObjects
group x by new { x.data1, x.data2} into g //g now contains groups with unique data1 and data3 objects
let d3 = g.GroupBy(x=>x.data3) //create a subgroup for data3 (per group g)
where d3.Count() > 1 //only with multiple data3
from gx in d3 //flatten d3 groups
select gx.First(); //select the first object in the d3 subgroup