Combining properties into a list of items

I am having problems with a small piece of code.

I have a list of MapItem classes with two properties, Address and Html, and I need to combine the Html properties for each element with the identical Address property For example:

firstMapItem = new MapItem { Address = "1122 Elm Street", Html="<p>some html</p>" }; secondMapItem = new MapItem { Address = "1122 Elm Street", Html="<p>different html</p>" }; 

will become:

 firstMapItem.Address == "1122 Elm Street"; firstMapItem.Html == "<p>some html</p><p>different html</p>"; secondMapItem.Address == "1122 Elm Street"; secondMapItem.Html == "<p>some html</p><p>different html</p>"; 

This is what I have tried so far:

  foreach (MapItem item in mapItems) { var sameAddress = from m in mapItems where m.Address == item.Address select m; if (sameAddress.Count() > 1) { //tried inserting -> item.Html = ""; right here as well foreach (MapItem single in sameAddress) { item.Html += single.Html; } } } 

I probably make it more complicated than it should be.

Thanks in advance.

+4
source share
5 answers

If you are group by Address, you will receive only one element if you have elements with the same address. If it is OK, go to group by . However , if you need all the source elements, when connecting to Html you should do the following:

 var newMapItems = mapItems .Select(mi => new MapItem() { Address = mi.Address, Html = mapItems.Where(mi2 => mi2.Address == mi.Address) .Select(mi3 => mi3.Html) .Aggregate((acc, html) => acc += html) } ); 
+2
source

You can group by Address , and then combine the Html values:

 var results = from m in mapItems group m by m.Address into ms select new MapItem { Address = ms.Key, Html = string.Concat(ms.Select(m => m.Html)) }; 
+5
source

Use grouping by address and then just string.Join Html of all the elements in the group to create a new MapItem :

 var resultList = mapItems.GroupBy(m => m.Address) .Select(g => new MapItem() { Address = g.Key, Html = string.Join("", g.Select(x => x.Html)) }) .ToList(); 

Edit:

Like the other solutions presented so far above, duplicates will be deleted - this does not look like what you want - below the solution that creates a list that is not deduplicated (so that 2 items will be returned for entering the sample)

 var resultList = mapItems.GroupBy(m => m.Address) .Select(g => g.Select( item => new MapItem() { Address = g.Key, Html = string.Join("", g.Select(x => x.Html)) } )) .SelectMany( x=> x) .ToList(); 
+3
source

You can do this with GroupBy and Select :

  var result = items .GroupBy(m => m.Address, m => m.Html) .Select(g => new MapItem() {Address = g.Key, Html = string.Concat(g.Select(h => h))}); 
0
source

This code should update existing objects with added values.

 foreach (MapItem item in mapItems) { var sameAddress = from m in mapItems group m by m.Address into ms select string.Join("", ms.Select(e => e.Html).ToArray()); foreach (string concatHtml in sameAddress) { item.Html = concatHtml; } } 
0
source

Source: https://habr.com/ru/post/1346983/


All Articles