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

时间:2014-12-11 13:56:57

标签: c# linq entity-framework

根据标题,可以将以下代码编写为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查询中完成,那将会很棒。

4 个答案:

答案 0 :(得分:2)

你只能消除一个内循环:

        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);
                }
            }
        }

答案 1 :(得分:1)

试试这个:

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));
});

答案 2 :(得分:1)

首先回到团队成员。创建成对的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());

答案 3 :(得分:0)

你在做什么在我看来就像一个简单的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是否可以正确翻译此查询。特别是GroupBy之后的选择。