LINQ to Entities - 将所有相关实体字段放入字符串中

发布于 2025-01-02 04:19:10 字数 210 浏览 3 评论 0原文

我有一个类似的人实体,

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 技术交流群。

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

发布评论

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

评论(3

手心的海 2025-01-09 04:19:10

您可以使用 Enumerable.Aggregate 轻松连接序列中的多个字符串方法。

在您的情况下,您必须首先将 House 实体列表投影到 House.HouseType 字符串列表中,然后聚合 将它们合并为一个字符串:

var houseTypes = person.Houses.Select(i => i.HouseType).AsEnumerable();
var emptyResult = "0";

return houseTypes.Any() ?
    houseTypes.Select(i => i.ToString())
              .Aggregate((current, next) => current + ", " + next) :
    emptyResult;

或者您可以简单地说:

var houseTypes = person.Houses
                       .Select(i => i.HouseType)
                       .AsEnumerable()
                       .Select(i => i.ToString());
return String.Join(", ", houseTypes);

houseTypes 序列为空时,它将返回一个空字符串

更新:

如果您使用的是 Entity Framework 4 或更高版本,则可以使用内置 SQL 之一函数直接在数据库中执行字符串转换:

var houseTypes = person.Houses
                       .Select(i => SqlFunctions.StringConvert((double)i.HouseType))
                       .AsEnumerable()
return String.Join(", ", houseTypes);

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 of House.HouseType strings and then aggregate them into a single string:

var houseTypes = person.Houses.Select(i => i.HouseType).AsEnumerable();
var emptyResult = "0";

return houseTypes.Any() ?
    houseTypes.Select(i => i.ToString())
              .Aggregate((current, next) => current + ", " + next) :
    emptyResult;

Alternatively you could simply say:

var houseTypes = person.Houses
                       .Select(i => i.HouseType)
                       .AsEnumerable()
                       .Select(i => i.ToString());
return String.Join(", ", houseTypes);

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:

var houseTypes = person.Houses
                       .Select(i => SqlFunctions.StringConvert((double)i.HouseType))
                       .AsEnumerable()
return String.Join(", ", houseTypes);
中二柚 2025-01-09 04:19:10

你的意思是这样的?:

var data = context.People.Where(p => p.PersonId == <id>).SelectMany(p => p.Houses).Select(h => h.HouseType);
var result = string.Join(<separator>, data);

You mean something like?:

var data = context.People.Where(p => p.PersonId == <id>).SelectMany(p => p.Houses).Select(h => h.HouseType);
var result = string.Join(<separator>, data);
萌吟 2025-01-09 04:19:10
var houseTypes = person.Houses
    .Select(i => i.HouseType).ToList();

return string.Join(" ", houseTypes.Select(x=>x.ToString()));
var houseTypes = person.Houses
    .Select(i => i.HouseType).ToList();

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