Linq问题集团

时间:2016-11-18 12:42:12

标签: c# linq

我有一个物品清单。该课程如下:

public class DeviceControllerDoorInfo
{

    [DataMember]
    public string DeviceControllerId { get; set; }
    [DataMember]
    public string DeviceControllerName { get; set; }
    [DataMember]
    public string DoorId { get; set; }
    [DataMember]
    public string DoorName { get; set; }

}

数据看起来像:

DoorId   DoorName  ControllerId ControllerName
------   --------  ------------ --------------

Door1    DoorOne   C1           C1
Door2    DoorTwo   C1           C1
Door3    DoorThree C2           C2

我希望它被转换,看起来像它。

public class AccessGroupControllerDoorEntity
{
    public string ControllerId { get; set; }
    public string ControllerName { get; set; }

    public List<AccessGroupDoorEnity> Doors { get; set; }
}

public class AccessGroupDoorEnity
{
    public string DoorId { get; set; }
    public string DoorName { get; set; }
}

ControllerId分组,然后列出门项目。

怎么做?

我试过了:

            var controllersId = allDoors.GroupBy(e => e.DeviceControllerId).Select(x => x);

但它是否将门作为列表?

我不确定。请帮忙。

更新

在我的数据服务中

    private AccessGroupEntity ConvertDoorsToEntity(DeviceControllerDoorInfo[] allDoors)
    {
        // return null if the object is invalid.
        if (allDoors != null)
        {
            AccessGroupEntity entity = new AccessGroupEntity();

            entity.ControllerDoorItems = new List<AccessGroupControllerDoorEntity>();

            //Convert to requested format instead of the below

            foreach (var door in allDoors)
            {
                entity.DoorItems.Add(new DoorInfoEntity
                {
                    DoorId = door.DoorId,
                    DoorName = door.DoorName,
                    ControllerId = door.DeviceControllerId,
                    ControllerName = door.DeviceControllerName
                });

            }

             return entity;

但我希望entity.ControllerDoorItems包含以下分组数据:

  ControllerId -> C1
  ControllerName -> C1
  Doors ->          2 Objects

2 个答案:

答案 0 :(得分:0)

DoorId   DoorName  ControllerId ControllerName
------   --------  ------------ --------------

Door1    DoorOne   C1           C1
Door2    DoorTwo   C1           C1
Door3    DoorThree C2           C2

当您使用GroupBy(e => e.ControllerId)它将返回两个列表(对于上面的示例),其中第一个列表有两个项ControllerId C1 ,另一个列表有一个其ControllerId为 C2 的项目。

因此GroupBy返回Lists而不是Items。

有关更新的问题:

List<AccessGroupControllerDoorEntity> grouped = allDoors.GroupBy(e => e.ControllerId)
        .Select(group => new AccessGroupControllerDoorEntity
                       {
                            DeviceControllerId = group.Key.Id,
                            DeviceControllerName = group.Key.Name,
                            Doors = group.ToList() 
                       });

答案 1 :(得分:0)

你可以尝试

         var list= allDoors.GroupBy(x=>new {x.ControllerId ,Name=ControllerName},
            (key, group) => new AccessGroupControllerDoorEntity
             { 
                ControllerId=Key.ControllerId ,
                ControllerName=Key.ControllerName,
                Doors = group.ToList()
             }))
            .ToList();