Groupby in LINQ and set the property

I have a class like this:

public class Foo { public string Regn{get;set;} public string DocName{get;set;} ... } 

In my application, this class uses with IEnumerable:

 IEnumerable<Foo> items; 

How to get a new IEnumerable, where for all elements with the same Regn and DocName property, DocName is set like this (only if objects with the same DocName> 1):

 item.DocName=item.DocName+".1";//+"2",etc. 

[UPDATE] Input example:

 Regn DocName 1 1 1 2 1 2 2 5 2 5 2 6 

Output:

 Regn DocName 1 1 1 2.1 1 2.2 2 5.1 2 5.2 2 6 
+4
source share
4 answers

If you have a default constructor for Foo, try using this:

 var newItems = items. GroupBy(f => Tuple.Create(f.Regn, f.DocName)). SelectMany(gr => gr.Count()<=1 ? gr : gr.Select((f, i) => new Foo { Regn = f.Regn, DocName = f.DocName + "." + (i + 1) })); 
+2
source

You can group LINQ and groups from a list that have only one item, and then DocName over the items in each group to set the DocName :

 // Group and filter var groups = items.GroupBy(i => new { i.Regn, i.DocName }) .Where(g => g.Count() > 1); // Iterate over each group with many items foreach (var g in groups) { var itemsInGroup = g.ToArray(); // Iterate over the items and set DocName for (var i = 0; i < itemsInGroup.Length; ++i) { itemsInGroup[i].DocName = g.Key + "." + (i + 1); } } 
+1
source

All in one statement, just for fun.

 var query = items.GroupBy(i => new { i.DocName, i.Regn }) .SelectMany(group => { int itemNum = 0; return group.Select(item => { var suffix = itemNum > 0 ? ("." + itemNum) : ""; var newDocName = item.DocName + suffix; itemNum++; return new { item, NewDocName = newDocName }; }); }); 
0
source

Or use the LINQ statement to create a new result set, for example:

 var fixedSet = from entry in existing group entry by entry.DocName + entry.Regn into groupedEntries let groupedEntriesAsArray = groupedEntries.ToArray() from groupedEntry in groupedEntriesAsArray let index = Array.IndexOf(groupedEntriesAsArray, groupedEntry) select new Foo { DocName = string.Format("{0}.{1}", groupedEntry.DocName, index + 1), Regn = string.Format("{0}.{1}", groupedEntry.Regn, index + 1) }; 
-1
source

All Articles