连接项目列表中的属性
我在一小部分代码上遇到了麻烦。
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以按
Address
进行分组,然后连接Html
值:You could group by
Address
and then concatenate theHtml
values:对地址进行分组,然后只需
string.Join
组中所有项目的 Html 即可生成新的MapItem
:编辑:
与迄今为止提出的其他解决方案一样,上述方法将删除重复项 - 这似乎不是您想要的 - 下面的解决方案创建了一个未进行重复数据删除的列表(因此将为示例输入生成 2 个项目)
Use a grouping on the address, then just
string.Join
the Html of all the items in the group to produce a newMapItem
: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)
如果您
按
地址分组,则当您有具有相同地址的项目时,您最终将只有一个项目。如果可以,请使用分组依据
。 但是,如果您需要所有原始项目,并连接 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 withGroup By
. However, if you need all the original items, with the Html concatenated, you should do like that:您可以使用
GroupBy
和Select
来完成此操作:You can do this with a
GroupBy
andSelect
:此代码应使用附加值更新现有对象。
This code should update your existing objects with appended values.