LINQ查询以连接多个表并在单行中获取逗号分隔的值

编程入门 行业动态 更新时间:2024-10-25 17:22:42
本文介绍了LINQ查询以连接多个表并在单行中获取逗号分隔的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在下表中列出了这些值.

帐户:

Id 名称电子邮件
101纳西尔·乌丁 nasir@email

角色:

Id 标题
101 管理员
102 操作员

AccountRole:

AccountId RoleId
101 101
101 102

现在,我想编写一个linq,使其结果如下:

UserAccount

AccountId 名称电子邮件角色
101 纳西尔·乌丁 nasir@email 管理员、操作员

要获得以上结果,我已在LINQ中编写了以下查询.但却没有得到预期的结果.

var userAccount1 =(来自_db.Accounts中的帐户在account.Id上的_db.AccountRoles中加入accountRole等于accountRole.AccountIdaccountRole.RoleId上_db.Roles中的join角色等于role.Id选择新的UserAccountInfo{AccountId = account.Id,名称= account.UserFullName,电子邮件=帐户.电子邮件,角色= string.Join(,",role.Title)});

解决方案

下面根据您帖子中提供的信息(以及一些假设,因为我找不到例如),使用Lambda表达式(而非查询表达式)为您提供了预期的结果您的任何表中的 UserFullName )

注意:我也相信有一种更有效的方法可以做到这一点,但这是没有其他条件的起点.

(正在使用.NET以下小提琴:

I have below tables with the values.

Account:

Id Name Email
101 Nasir Uddin nasir@email

Role:

Id Title
101 Admin
102 Operator

AccountRole:

AccountId RoleId
101 101
101 102

Now I want to write a linq to have the result like below:

UserAccount

AccountId Name Email Roles
101 Nasir Uddin nasir@email Admin, Operator

To get the above result I have written the below query in LINQ. But it does not get the expected result.

var userAccount1 = (from account in _db.Accounts join accountRole in _db.AccountRoles on account.Id equals accountRole.AccountId join role in _db.Roles on accountRole.RoleId equals role.Id select new UserAccountInfo { AccountId = account.Id, Name = account.UserFullName, Email = account.Email, Roles = string.Join(",", role.Title) });

解决方案

The below gives you the expected result using Lambda Expression (not Query Expression) based on the information provided in your post (and some assumptions since I could not find e.g. UserFullName in any of your tables)

Note: I'm also convinced there is a more efficient way to do this, but it is a starting point if nothing else.

(Here is working .NET Fiddle of the below: dotnetfiddle/aGra15):

// Join the AccountRoles and Roles together and group all Titles for // a given AccountId together var groupedAccountRoles = AccountRoles.GroupJoin(Roles, i => i.RoleId, o => o.Id, (o, i) => new {o, i}) .Select(x => new {AccountId = x.o.AccountId, Titles = string.Join(",", x.i.Select(y => y.Title))}); // Perform another GroupJoin to group by AccountId and Join to groupedAccountRoles table. Then `string.Join()` var userAccount1 = Accounts.GroupJoin(AccountRoles, acc => acc.Id, accrol => accrol.AccountId, (o, i) => new {o, UserAccountRoles = i}) .GroupJoin(groupedAccountRoles, ii => ii.o.Id, oo => oo.AccountId, (ii, oo) => new UserAccountInfo { AccountId = ii.o.Id, Email = ii.o.Email, Name = ii.o.Name, Roles = string.Join(",", oo.Select(x => x.Titles)) });

This will give the following output:

更多推荐

LINQ查询以连接多个表并在单行中获取逗号分隔的值

本文发布于:2023-10-28 18:41:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537449.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   并在   逗号   LINQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!