我有一個項目列表。這個類看起來像:集團通過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
您將需要使用的GroupBy當你在標題中所述。要麼顯示一些你的自己的努力,要麼指明你的問題到底是什麼? – CSharpie
這裏有什麼問題?請明確說明你在問什麼。你問如何讓'controllersId'成爲一個列表?你在問如何分組數據嗎?如果是這樣,那麼請告訴我們你希望它是什麼樣子「看起來像它」是不是一個很好的描述,因爲沒有指定「它」。 「怎麼做?」也不是一個好的問題描述。這裏的「它」是什麼? –
@ LasseV.Karlsen請找到最新的問題。 – StrugglingCoder