连接项目列表中的属性

发布于 2024-10-31 00:41:29 字数 1346 浏览 1 评论 0原文

我在一小部分代码上遇到了麻烦。

我有一个 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;
                    }
                }
            }

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

提前致谢。

I am having trouble with a small section of code.

I have a List of a MapItem class with a couple properties, Address and Html, and I need to concatenate the Html properties for each item with an 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>" };

would 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 am probably making this more complicated than it needs to be.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暖树树初阳… 2024-11-07 00:41:29

您可以按 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))
              };

You could group by Address and then concatenate 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))
              };
人生百味 2024-11-07 00:41:29

对地址进行分组,然后只需 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();

Use a grouping on the address, then just string.Join the Html of all the items in the group to produce 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 approach will remove duplicates - that doesn't seem to be what you want - below a solution that creates a list that is not deduplicated (so will produce 2 items for the sample input)

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();
爱殇璃 2024-11-07 00:41:29

如果您地址分组,则当您有具有相同地址的项目时,您最终将只有一个项目。如果可以,请使用分组依据但是,如果您需要所有原始项目,并连接 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) 
                                }
           );

If you group by Address, you'll end up with only one item when you have items with the same Address. If that's OK with, go with Group By. However, if you need all the original items, with the Html concatenated, you should do like that:

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) 
                                }
           );
瞎闹 2024-11-07 00:41:29

您可以使用 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))});

You can do this with a 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))});
撩人痒 2024-11-07 00:41:29

此代码应使用附加值更新现有对象。

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

            }

This code should update your existing objects with appended 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;
                }

            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文