GroupBy in LINQ and set property
I have such class:
public class Foo
{
public string Regn{get;set;}
public string DocName{get;set;}
...
}
In the my application this class uses with IEnumerable:
IEnumerable<Foo> items;
How to get new IEnumerable, where for all items with the same Regn and DocName property DocName sets开发者_如何学JAVA like this(only if objects with same DocName >1):
item.DocName=item.DocName+".1";//+"2",etc.
[UPDATE] Input sample:
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
If you have a default constructor for Foo try to use 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)
}));
You can group with LINQ and cast out groups that only have one item, then iterate 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);
}
}
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 };
});
});
Or use a LINQ statement to create a new result set like:
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)
};
精彩评论