C# 林克集团发行
我有一份物品清单。该类看起来像:C# 林克集团发行,c#,linq,C#,Linq,我有一份物品清单。该类看起来像: public class DeviceControllerDoorInfo { [DataMember] public string DeviceControllerId { get; set; } [DataMember] public string DeviceControllerName { get; set; } [DataMember] public string DoorId { get; set;
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 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; }
}
但它是否将门列为列表
我不确定。请帮忙
更新
在我的数据服务中
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;
使用GroupBy(e=>e.ControllerId)
时,它将返回两个列表(对于上面的示例),第一个列表有两个项目,其中ControllerId
为C1,另一个列表有一个项目,其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()
});
List group=allDoors.GroupBy(e=>e.ControllerId)
.选择(组=>new AccessGroupControllerDoorEntity
{
DeviceController Id=group.Key.Id,
DeviceController名称=group.Key.Name,
Doors=group.ToList()
});
您可以试试
var list= allDoors.GroupBy(x=>new {x.ControllerId ,Name=ControllerName},
(key, group) => new AccessGroupControllerDoorEntity
{
ControllerId=Key.ControllerId ,
ControllerName=Key.ControllerName,
Doors = group.ToList()
}))
.ToList();
您需要按照标题中的说明使用GroupBy。要么表现出你自己的努力,要么具体说明你到底有什么问题?这里的问题是什么?请确切说明你的问题。您是否在询问如何将
控制器ID
作为列表?您是否在询问如何对数据进行分组?如果是这样的话,请告诉我们您希望它看起来像什么。“看起来像它”不是一个好的描述,因为“它”没有指定。“怎么做?”也不是一个好的问题描述。这里的“it”是什么?@LasseV.Karlsen请查找更新的问题。@StruglingCoder检查我的答案。Please@I我们已经更新了问题。您能找到吗?但DeviceController DoorInfo不包含ControllerId,它包含DeviceController Id,但group.key.Id抛出错误。我猜,因为它返回了两个door对象,所以对于Key.Id或Key.Name没有意义。那么你应该使用DeviceController,这是一个如何使用它的例子。请注意,AccessGroupControllerDoorEntity
的DeviceControllerId
不能是主键。
var list= allDoors.GroupBy(x=>new {x.ControllerId ,Name=ControllerName},
(key, group) => new AccessGroupControllerDoorEntity
{
ControllerId=Key.ControllerId ,
ControllerName=Key.ControllerName,
Doors = group.ToList()
}))
.ToList();