我如何linq对象分组

时间:2019-02-22 09:31:06

标签: c# mongodb linq mongodb-query grouping

如何对这些对象进行分组?我可以通过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; }
}

2 个答案:

答案 0 :(得分:1)

如果您使用外键建立一对多关系,并且想要带有子项的项目,例如学校及其学生,客户其订单,主组及其子组,则可以使用一个简单的选择即可获得结果,或使用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

GroupJoin

Enumerable.GroupJoin效率更高(或等效于IQueryable)。可枚举版本使用字典来查看它是否已经找到带有该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(),
    });

答案 1 :(得分: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))
});