C# 按列表分组包含另一个 class
C# group by a list contains another class
我有两个 类 如下:
public class result
{
public person identity { get; set; }
}
public class person
{
public string name { get; set; }
public string family { get; set; }
}
我有 List<result>
其中有一些重复数据。我想通过 System.Linq.GroupBy
摆脱重复数据
我搜索了很多,我认为我找到的最佳答案是 here,所以我模拟了我的代码如下:
class Program
{
static void Main(string[] args)
{
person p1 = new person();
p1.name = "Bill"; p1.family = "Gates";
person p2 = new person();
p2.name = "Satoshi"; p2.family = "Nakamoto";
person p3 = new person();
p3.name = "AmirAli"; p3.family = "Sam";
List<result> lst = new List<result>();
List<List<result>> lstGrp = new List<List<result>>();
lst.Add(new result { identity = p1 });
lst.Add(new result { identity = p1 });
lst.Add(new result { identity = p1 });
lst.Add(new result { identity = p2 });
lst.Add(new result { identity = p2 });
lst.Add(new result { identity = p2 });
lst.Add(new result { identity = p3 });
lst.Add(new result { identity = p3 });
lst.Add(new result { identity = p3 });
lstGrp = lst
.GroupBy(r => r.identity)
.Select(grp => grp.ToList())
.ToList();
foreach (List<result> list in lstGrp)
{
foreach (result item in list)
{
Console.WriteLine(item.identity.name + " " + item.identity.family);
}
}
}
}
如图所示,我在不同的结果中有重复的人,但我想在我的控制台中看到每个人一次:
但是我最后面对的是:
我应该如何更改我的代码以获得我想要的结果?
非常感谢。
我认为您真正要找的是“distinct”而不是“groupBy”。
请参阅此处 Select distinct using linq .
https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct?view=net-5.0
他们对 distinct 的看法:
Returns distinct elements from a sequence.
示例:
person p1 = new person();
p1.name = "Bill";
p1.family = "Gates";
person p2 = new person();
p2.name = "Satoshi";
p2.family = "Nakamoto";
person p3 = new person();
p3.name = "AmirAli";
p3.family = "Sam";
List<result> lst = new List<result>();
List<person> lstGrp = new List<person>();
lst.Add(new result{identity = p1});
lst.Add(new result{identity = p1});
lst.Add(new result{identity = p1});
lst.Add(new result{identity = p2});
lst.Add(new result{identity = p2});
lst.Add(new result{identity = p2});
lst.Add(new result{identity = p3});
lst.Add(new result{identity = p3});
lst.Add(new result{identity = p3});
lstGrp = lst.Select(r => r.identity).Distinct().ToList();
foreach (var item in lstGrp)
{
Console.WriteLine(item.name + " " + item.family);
}
您已将所有 Name 和 Family 分配给同一个变量 p1
我有两个 类 如下:
public class result
{
public person identity { get; set; }
}
public class person
{
public string name { get; set; }
public string family { get; set; }
}
我有 List<result>
其中有一些重复数据。我想通过 System.Linq.GroupBy
我搜索了很多,我认为我找到的最佳答案是 here,所以我模拟了我的代码如下:
class Program
{
static void Main(string[] args)
{
person p1 = new person();
p1.name = "Bill"; p1.family = "Gates";
person p2 = new person();
p2.name = "Satoshi"; p2.family = "Nakamoto";
person p3 = new person();
p3.name = "AmirAli"; p3.family = "Sam";
List<result> lst = new List<result>();
List<List<result>> lstGrp = new List<List<result>>();
lst.Add(new result { identity = p1 });
lst.Add(new result { identity = p1 });
lst.Add(new result { identity = p1 });
lst.Add(new result { identity = p2 });
lst.Add(new result { identity = p2 });
lst.Add(new result { identity = p2 });
lst.Add(new result { identity = p3 });
lst.Add(new result { identity = p3 });
lst.Add(new result { identity = p3 });
lstGrp = lst
.GroupBy(r => r.identity)
.Select(grp => grp.ToList())
.ToList();
foreach (List<result> list in lstGrp)
{
foreach (result item in list)
{
Console.WriteLine(item.identity.name + " " + item.identity.family);
}
}
}
}
如图所示,我在不同的结果中有重复的人,但我想在我的控制台中看到每个人一次:
但是我最后面对的是:
我应该如何更改我的代码以获得我想要的结果?
非常感谢。
我认为您真正要找的是“distinct”而不是“groupBy”。 请参阅此处 Select distinct using linq .
https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct?view=net-5.0
他们对 distinct 的看法:
Returns distinct elements from a sequence.
示例:
person p1 = new person();
p1.name = "Bill";
p1.family = "Gates";
person p2 = new person();
p2.name = "Satoshi";
p2.family = "Nakamoto";
person p3 = new person();
p3.name = "AmirAli";
p3.family = "Sam";
List<result> lst = new List<result>();
List<person> lstGrp = new List<person>();
lst.Add(new result{identity = p1});
lst.Add(new result{identity = p1});
lst.Add(new result{identity = p1});
lst.Add(new result{identity = p2});
lst.Add(new result{identity = p2});
lst.Add(new result{identity = p2});
lst.Add(new result{identity = p3});
lst.Add(new result{identity = p3});
lst.Add(new result{identity = p3});
lstGrp = lst.Select(r => r.identity).Distinct().ToList();
foreach (var item in lstGrp)
{
Console.WriteLine(item.name + " " + item.family);
}
您已将所有 Name 和 Family 分配给同一个变量 p1