我怎样才能linq对象分组

问题描述 投票:1回答:2

我该如何分组这些对象?我可以通过LINQ并绑定它。但这没用,因为我需要反向分组。像那样 :

MainGroup>组列表>子组列表

也许我可以使用LINQ查询首先获取值。我不知道。

我是LINQ的入门用户。我没有太多信息。预先感谢您的帮助。我得到并绑定mongodb数据:

var subGroupCollection = Database.GetCollection<SubGroup>(typeof(SubGroup).Name);
var groupCollection = Database.GetCollection<Group>(typeof(Group).Name);
var mainGroupCollection = Database.GetCollection<MainGroup>(typeof(MainGroup).Name);

var query = from sg in subGroupCollection.AsQueryable()
            join mg in mainGroupCollection on sg.MainGroupId equals mg.Id into mainGroups
            join z in groupCollection on sg.GroupId equals z.Id into groups
            select new SoccerOddType
            {
                Id = sg.Id,
                IsActive = sg.IsActive,
                GroupId = sg.GroupId,
                Name = sg.Name,
                LastUpdateDate = sg.LastUpdateDate,
                CreatedDate = sg.CreatedDate,
                Order = sg.Order,
                Discount = sg.Discount,
                DiscountType = sg.DiscountType,
                MainGroupId = sg.MainGroupId,
                MainGroup = mainGroups.First(),
                Group = groups.First()
            };

来自:

public class MainGroup
{
    public string Id { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
}

public class Group
{
    public string Id { get; set; }
    public string MainGroupId { get; set; }
    [BsonIgnore] public Group MainGroup { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
}

public class SubGroup
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string MainGroupId { get; set; }
    public string GroupId { get; set; }
    [BsonIgnore] public Group MainGroup { get; set; }
    [BsonIgnore] public Group Group { get; set; }
    public bool IsActive { get; set; }
    public decimal Discount { get; set; }
    public EnmDiscountType DiscountType { get; set; }
}

至 :

public class MainGroupViewModel
{
    public string Id { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
    public List<GroupViewModel> Groups { get; set; }
}

public class GroupViewModel
{
    public string Id { get; set; }
    public string MainGroupId { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
    public List<SubGroupViewModel> SubGroups { get; set; }
}

public class SubGroupViewModel
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string MainGroupId { get; set; }
    public string GroupId { get; set; }
    public bool IsActive { get; set; }
    public decimal Discount { get; set; }
    public EnmDiscountType DiscountType { get; set; }
}
c# mongodb linq mongodb-query grouping
2个回答
1
投票

如果你有一对多的关系,使用外键,并且你想要他们的子项目,例如学校与他们的学生,客户与他们的订单,MainGroups与他们的子组,那么你可以使用一个简单的选择得到你的结果,或使用GroupJoin

选择

var result = mainGroupCollection.Select(mainGroup => new MainGroupViewModel
{
    Id = mainGroup.Id,
    Name = mainGroup.Name,
    ...

    // Keep only the Groups of this MainGroup, using foreign key
    Groups = groupCollection
        .Where(group => group.MainGroupId == mainGroup.Id)
        .Select(group => new GroupViewModel
        {
            Id = group.Id,
            Name = group.Name,
            ...

            // Keep only the subGroups of this Group, using foreign key
            SubGroups = subGroupCollection
                .Where(subGroup => subGroup.GroupId == group.Id)
                .Select(subGroup => new SubGroupViewModel
                {
                    Id = group.Id,
                    Name = group.Name,
                    ...
                })
                .ToList(),
        })
        .ToList(),
});

尽管此方法有效,但效率不高,因为对于mainGroupCollection中的每个元素,它必须枚举整个GroupCollection,并且必须枚举整个SubGroupCollection的每个元素。

根据您从数据库查询时使用的DBMS,这不是一个大问题。但是,我会选择GroupJoin

群组加入

Enumerable.GroupJoin更有效率(或相当于IQueryable)。 Enumerable版本使用Dictionary来查看它是否已找到具有此Id的项目,因此不需要在每个集合上多次枚举。

// GroupJoin the mainGroupCollection with the groupCollection:
var result = mainGroupCollection.GroupJoin(groupCollection,
    mainGroup = mainGroup.Id,         // from every mainGroup take the primary key
    group => group.MainGroupId,       // from every group take the foreign key

    // ResultSelector: for every mainGroup and its groups make one MainGroupViewModel
    (mainGroup, groupsOfThisMainGroup) => new MainGroupViewModel
    {
        Id = mainGroup.Id,
        Name = mainGroup.Name,
        ...

        // for the Groups: GroupJoin the groups of this main group with the subGroups
        Groups = groupsOfThisMainGroup.GroupJoin(subGroupCollection,
            groupOfThisMainGroup => groupOfThisMainGroup.Id,
            subGroup => subGroup.GroupId,

            // result selector
            (group, subGroupsOfThisGroup) => new GroupViewModel
            {
                Id = group.Id,
                Name = group.Name,

               SubGroups = subGroupsOfThisGroup.Select(subGroup => new SubGroupViewModel
               {
                    Id = subGroup.Id,
                    Name = subGroup.Name,
                    ...
               })
               .ToList(),
        })
        .ToList(),
    });

0
投票

我可能会选择.Select()

var subGroups = subGroupCollection.Select(sg => new SubGroupViewModel
{
    Id = sg.Id,
    Name = sg.Name,
    MainGroupId = sg.MainGroupId,
    GroupId = sg.GroupId,
    IsActive = sg.IsActive,
    Discount = sg.Discount,
    DiscountType = sg.DiscountType
});

var groups = groupCollection.Select(g => new GroupViewModel 
{
    Id = g.Id,
    MainGroupId = g.MainGroupId,
    Name = g.Name,
    IsActive = g.IsActive,
    SubGroups = subGroups.Where(sg => sg.GroupId == g.Id).ToList() 
});

var mainGroups = mainGroupCollection.Select(mg => new MainGroupViewModel
{
    Id = mg.Id,
    Name = mg.Name,
    IsActive = mg.IsActive,
    // .Any() or .All() here?
    Groups = groups.Where(g => g.SubGroups.Any(sg => sg.MainGroupId == mg.Id))
});
© www.soinside.com 2019 - 2024. All rights reserved.