将项列表中的属性连接起来



我对一小段代码有问题。

我有一个MapItem类的列表,其中包含两个属性,Address和Html,并且我需要将每个项目的Html属性与相同的Address属性连接起来例如:

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

将成为:

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>";

这是我到目前为止所尝试的:

            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;
                    }
                }
            }

我可能使它比它需要的更复杂。

您可以按Address分组,然后将Html的值连接起来:

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))
              };

在地址上使用分组,然后仅string.Join组中所有项目的Html生成新的MapItem:

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

与目前为止提供的其他解决方案一样,上面的方法将删除重复项—这似乎不是您想要的—下面的解决方案创建一个未重复数据删除的列表(因此将为示例输入生成2项)

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();

如果您group by地址,当您拥有具有相同地址的项目时,您将最终获得只有一个项目。如果这是OK的,去Group By然而,如果你需要所有的原始项目,与Html连接,你应该这样做:

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) 
                                }
           );

您可以使用GroupBySelect:

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

这段代码应该用附加的值更新你现有的对象。

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;
                }
            }

相关内容

  • 没有找到相关文章

最新更新