2016-11-18 91 views
-1

我有一个项目列表。这个类看起来像:集团通过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); 

但它使门的名单?

我不知道。请帮忙。

UPDATE

在我的数据服务

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 
+3

您将需要使用的GroupBy当你在标题中所述。要么显示一些你的自己的努力,要么指明你的问题到底是什么? – CSharpie

+2

这里有什么问题?请明确说明你在问什么。你问如何让'controllersId'成为一个列表?你在问如何分组数据吗?如果是这样,那么请告诉我们你希望它是什么样子“看起来像它”是不是一个很好的描述,因为没有指定“它”。 “怎么做?”也不是一个好的问题描述。这里的“它”是什么? –

+0

@ LasseV.Karlsen请找到最新的问题。 – StrugglingCoder

回答

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

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

当您使用GroupBy(e => e.ControllerId)它将Ret瓮两个列表(对于上面的例子),其中,所述第一列表有两个项目在哪里ControllerIdC1并且与一个项目,其是ControllerId C2 另一个列表。

所以返回的GroupBy列表和不属于项。

为您更新的问题:

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

@我更新了这个问题。你能找到吗? – StrugglingCoder

+0

我更新了我的回答 –

+0

但DeviceControllerDoorInfo不包含ControllerId,它包含DeviceControllerId但是然后group.key.Id抛出错误。我想,因为它返回两个门对象,因为Key.Id或Key.Name没有意义。能否请你帮忙。 – StrugglingCoder

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