C# 查找具有重复键但值不同的项
我有一个包含多个项目的列表。每个项目由三个部分组成。ID、名称、值。我只需要得到ID和Name相等但值不同的项C# 查找具有重复键但值不同的项,c#,C#,我有一个包含多个项目的列表。每个项目由三个部分组成。ID、名称、值。我只需要得到ID和Name相等但值不同的项 public class MyModel { public int ElementID { get; set; } public string ElementName { get; set; } public string ElementValue { get; set; } } public class Program { static void M
public class MyModel
{
public int ElementID { get; set; }
public string ElementName { get; set; }
public string ElementValue { get; set; }
}
public class Program
{
static void Main(string[] args)
{
var list = new List<MyModel>();
list.Add(new MyModel {ElementID = 1, ElementName = "Id1Name1", ElementValue = "Id1Name1Value1"});
list.Add(new MyModel {ElementID = 1, ElementName = "Id1Name1", ElementValue = "Id1Name1Value2"});
list.Add(new MyModel {ElementID = 1, ElementName = "Id1Name2", ElementValue = "Id1Name2Value1"});
list.Add(new MyModel {ElementID = 1, ElementName = "Id1Name3", ElementValue = "Id1Name3Value1"});
list.Add(new MyModel {ElementID = 1, ElementName = "Id1Name3", ElementValue = "Id1Name3Value2"});
list.Add(new MyModel {ElementID = 1, ElementName = "Id1Name4", ElementValue = "Id1Name4Value1"});
list.Add(new MyModel {ElementID = 1, ElementName = "Id1Name4", ElementValue = "Id1Name4Value1"});
list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name1", ElementValue = "Id2Name1Value1"});
list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name1", ElementValue = "Id2Name1Value2"});
list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name2", ElementValue = "Id2Name2Value1"});
list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name3", ElementValue = "Id2Name3Value1"});
list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name3", ElementValue = "Id2Name3Value2"});
list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name4", ElementValue = "Id2Name4Value1"});
list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name5", ElementValue = "Id2Name5Value1"});
list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name5", ElementValue = "Id2Name5Value2"});
list.Add(new MyModel {ElementID = 2, ElementName = "Id2Name5", ElementValue = "Id2Name5Value3"});
//Expected output
//Id1Name1Value1
//Id1Name1Value2
//Id1Name3Value1
//Id1Name3Value2
//Id2Name1Value1
//Id2Name1Value2
//Id2Name3Value1
//Id2Name3Value2
//Id2Name5Value1
//Id2Name5Value2
//Id2Name5Value3
}
}
这是显而易见的,每个组的元素值都不同。我将创建一个扩展方法,该方法将返回一个列表,其中包含重复但值不同的项。我正在考虑一个通用版本,在这个版本中,您可以定义对象上应该作为组的哪些属性,以及使用哪些属性作为值。也许您可以使用属性上的属性来定义它们是在组中使用还是作为值使用。我们将看到…选择具有多个值的id/名称组合:
list.GroupBy(i =>
new
{
i.ElementID,
i.ElementName
}).
Where(g => g.Select(i => i.ElementValue).Distinct().Count() > 1);
请注意,这将返回包含您正在查找的项目的组,每个组都有其项目
对于这样一个简单的问题,您可以使用以下内容:
list.GroupBy(x => x.ElementValue).Select(grp => grp.First());
list.ForEach(x => Console.WriteLine(x.ElementValue));
为我产生了这个输出
您需要考虑的最大问题是,如果要进行大量的相等比较,您应该创建一个自定义的IEqualityComparer类,并确保正确实现GetHashCode方法。“我没有正确使用lambda表达式”你的问题中没有lambda表达式…你能给我们看看你试过的lambda表达式吗?干得好!谢谢!元素值上的Distinct实现了这一点。
list.GroupBy(x => x.ElementValue).Select(grp => grp.First());
list.ForEach(x => Console.WriteLine(x.ElementValue));