是否可以将其编写为 linq 查询

本文关键字:linq 查询 是否 | 更新日期: 2023-09-27 18:33:55

根据标题,是否可以将以下代码段编写为linq查询。在当前状态下,我发现它的性能不是很好,大约需要 1 到 2 秒才能完成几千行。

var user = this.userService.Find<ApplicationUser>(userid);
var dict = new Dictionary<string, List<int>>();
List<ApplicationUser> allUsers = new List<ApplicationUser>();
foreach (var partner in user.Partners)
{
    foreach (var programme in partner.Programmes)
    {
        foreach (var tm in programme.TeamMembers)
        {
            if (!dict.ContainsKey(tm.ApplicationUserId))
            {
                dict.Add(tm.ApplicationUserId, new List<int> {partner.Id});
                allUsers.Add(tm.ApplicationUser);
            }
            else
            {
                dict[tm.ApplicationUserId].Add(partner.Id);
            }
        }
    }
}
var usersviewmodel = Mapper.Map<List<UserListViewModel>>(allUsers);
usersviewmodel.ForEach(x=> x.PartnerIds = dict[x.Id]);

我不介意以后必须自动映射和添加 ID,但如果这也可以在 LINQ 查询中完成,那就太好了。

是否可以将其编写为 linq 查询

你只能消除一个内部循环:

        foreach (var partner in user.Partners)
        {
            foreach (var tm in partner.Programmes.SelectMany(programme => programme.TeamMembers)) {
                if (!dict.ContainsKey(tm.ApplicationUserId))
                {
                    dict.Add(tm.ApplicationUserId, new List<int> { partner.Id });
                    allUsers.Add(tm.ApplicationUser);
                }
                else
                {
                    dict[tm.ApplicationUserId].Add(partner.Id);
                }
            }
        }

试试这个:

var dict = new Dictionary<string, List<int>>();
List<ApplicationUser> allUsers = new List<ApplicationUser>();
var groupedByApplicationUserId = from partner in user.Partners
                                 from programme in partner.Programs
                                 from tm in programme.TeamMembers
                                 select new { ApplicationUserId = tm.ApplicationUserId,
                                              PartnerId = partner.Id,
                                              ApplicationUser = tm.ApplicationUser,
                                             };
groupedByApplicationUserId.GroupBy(item => item.ApplicationUserId).ToList().ForEach(group =>
{
    dict.Add(group.Key, group.Select(item => item.PartnerId).ToList());
    allUsers.AddRange(group.Select(item => item.ApplicationUser));
});

首先是团队成员。创建 ApplicationUserId s(如 TeamMembId )和 PartnerId s 对。当您按TeamMemberId分组并转换为字典时。

var userDict = user.Partners.SelectMany(
    p =>
        p.Programmes.SelectMany(
            prg =>
            prg.TeamMembers.Select(
                t => new { PartnerId = p.Id, TeamMembId = t.ApplicationUserId })))
         .GroupBy(r => r.TeamMembId)
         .ToDictionary(g => g.Key, g => g.Select(i => i.PartnerId).ToList());
在我看来,

你正在做的事情就像一个简单的GroupBy。

var query = 
    user.Partners
        .SelectMany(x=>x.Programmer)
        .SelectMany(x=>x.TeamMembers)
        .Select(x=> new { partner = partner.Id, applicationUser = tm.ApplicationUser })
        .GroupBy(x=>x.applicationUser)
        .Select(x=>new { x.applicationUser, partners = x.Select(y=>y.partner) }) ;

但我不确定 EF 是否可以正确翻译此查询。尤其是分组之后的选择。