LINQ to Entities - 将所有相关实体字段放入字符串中
我有一个类似的人实体,
PersonId
PersonName
PersonPhone
我有一个房屋实体,
HouseID
HouseType
HouseSize
这两个实体都与多对多关系相关。 我需要将一个人(有很多房子)的所有 HouseType 放入一个字符串中。
I have a Person Entity something like that
PersonId
PersonName
PersonPhone
I have a House Entity
HouseID
HouseType
HouseSize
Both Entities are related with many to many relation.
I need to have all the HouseType for a person (which has many houses) into a string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Enumerable.Aggregate 轻松连接序列中的多个字符串方法。
在您的情况下,您必须首先将
House
实体列表投影到House.HouseType
字符串列表中,然后聚合 将它们合并为一个字符串:或者您可以简单地说:
当
houseTypes
序列为空时,它将返回一个空字符串。更新:
如果您使用的是 Entity Framework 4 或更高版本,则可以使用内置 SQL 之一函数直接在数据库中执行字符串转换:
You can easily concatenate multiple strings from a sequence with the Enumerable.Aggregate method.
In your case you'll have to first project the list of
House
entities into a list ofHouse.HouseType
strings and then aggregate them into a single string:Alternatively you could simply say:
which will return an empty string when the
houseTypes
sequence is empty.Update:
If you're using Entity Framework 4 or above, you can use one of the built-in SQL functions to perform the conversion to string directly in the database:
你的意思是这样的?:
You mean something like?: