简体   繁体   English

循环获取对象C#中的重复项

[英]looping through to get duplicates in an object C#

I have an object of objects where one property can have duplicate values i am trying to find an algorithm to loop through the object and create a new list of object having group by duplicate values. 我有一个对象的对象,其中一个属性可以具有重复的值,我试图找到一种算法来遍历该对象,并创建一个具有重复值分组的对象的新列表。

how do i loop through an object and create a new object in C# i have a view model eg: 我如何遍历一个对象并在C#中创建一个新对象,我有一个视图模型,例如:

   pViewModel  {
    public itemFullName {get;set;}
    public Item Item{get;set;}
    public string itemAddress {get;set;}
    public string itemCountry {get;set;}
public string addressId {get;set;}
    }
    public Item{
    public int itemId{get;set;}
    }

I want to create a new object after finding matching fullname but different id so my new object will have a list of itemFullName, item.itemid(pipedelimited values for all the items in the previous list),itemaddress, itemCountry in it. 我想在找到匹配的全名但ID不同之后创建一个新对象,因此我的新对象将具有itemFullName,item.itemid(前一个列表中所有项目的管道分隔值),itemaddress,itemCountry的列表。

Any help will be awesome. 任何帮助都会很棒。 thank you 谢谢

someone pointed out to this 有人指出了这一点

var itemsAndIds = list
    .GroupBy(m => m.itemFullName, m => m.Item.itemId)
    .Select(g => new {ItemFullName = g.Key, ItemIds = string.Join("|", g)})

but now I need the new properties added to this object 但是现在我需要将新属性添加到该对象

This answer could help you: https://stackoverflow.com/a/5232194/1341189 这个答案可以帮助您: https : //stackoverflow.com/a/5232194/1341189

Given that ItemAddress and ItemCountry are always the same as ItemFullName in your example that means you can do something like this: 假设您的示例中的ItemAddress和ItemCountry始终与ItemFullName相同,这意味着您可以执行以下操作:

var itemsAndIds = list
    .GroupBy(m => new { 
               ItemFullName = m.itemFullName, 
               ItemAddress = m.itemAddress, 
               ItemCoutnry = m.itemCountry },
             m => m.Item.itemId)
    .Select(g => new {
              ItemFullName = g.Key.ItemFullName, 
              ItemIds = string.Join("|", g),
              ItemAddress = g.Key.ItemAddress,
              ItemCoutnry = g.Key.ItemCountry})

Also I would like to suggest that you read the Microsoft Naming Conventions. 我也建议您阅读Microsoft命名约定。

Property Names should be written in PascalCase (same goes for Class Names). 属性名称应使用PascalCase编写(类名称也是如此)。 https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-type-members https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-type-members

You could override bool Equals(Object obj) method in your object. 您可以在对象中重写bool Equals(Object obj)方法。 This way you can group by whole items and then just select new ones. 这样,您可以按整个项目分组,然后选择新的项目。 You will keep your comparation logic sealed in your object, co you can easily use build in mechanics like == comparation or whole linq magic without need of any hacks. 您将比较逻辑密封在对象中,可以轻松地使用==比较或linq魔术之类的内置机制,而无需任何技巧。 this way your class is fully compatible with SOLID principle 这样,您的课程就与SOLID原理完全兼容

keep in mind that to preserve consistency if you override Equals, it is also worth to override int GetHashCode() method. 请记住,如果要重写Equals,则要保持一致性,还应该重写int GetHashCode()方法。 this way you keep consistency of comparartion (to be precised you need to be sure that tour implementation follows those three main rules): 这样,您可以保持比较的一致性(确切地说,您需要确保游览实现遵循这三个主要规则):

  • a == a and a.Equals(a) should always be true (Reflexivity). a == a和a.Equals(a)应该始终为true(自反)。
  • a == b, b == a, a.Equals(b) and b.Equals(a) should always give the same result. a == b,b == a,a.Equals(b)和b.Equals(a)应始终给出相同的结果。 (Symmetry) (对称)
  • If a == b is true and b == c is true, then a == c should also be true (Transitivity). 如果a == b为真且b == c为真,则a == c也应为真(传递性)。 The same applies to a.Equals(b), b.Equals(c) and a.Equals(c). 同样适用于a.Equals(b),b.Equals(c)和a.Equals(c)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM