我有这门课:
public class ItemList
{
public int GuID { get; set; }
public int ItemID { get; set; }
public string Name { get; set; }
public entityType Status { get; set; }
public int Zone { get; set; }
public class Waypoint
{
public int SubID { get; set; }
public int Heading { get; set; }
public float PosX { get; set; }
public float PosY { get; set; }
public float PosZ { get; set; }
}
public List<Waypoint> Routes = new List<Waypoint>();
}
在此列表中使用的是: 公共(public)列表 myList = new List();
新项目添加到是这样的:
ItemList newItem = new ItemList();
newItem.GUID = GUID;
newItem.ItemID = ItemID;
newItem.Name = Name;
newItem.Status = Status;
// Inner Routes List
ItemList.Waypoint itemLocation = new ItemList.Waypoint();
itemLocation.SubID = SubID;
itemLocation.Zone = Zone;
itemLocation.Heading = convertHeading(Heading);
itemLocation.PosX = PosX;
itemLocation.PosY = PosY;
itemLocation.PosZ = PosZ;
itemLocation.Rest = Rest;
newItem.Routes.Add(itemLocation);
myList.Add(newItem);
现在我需要按 ItemID 对其进行分组,并加入每个相等 ItemID 的 Routes 的第一个条目。
这将是 public List<ItemList> myList = new List<ItemList>(); 的示例数据:
GUID ItemID ListOfRoutes
1 20 GUID_1_Routes
2 20 GUID_2_Routes
3 20 GUID_3_Routes
4 20 GUID_4_Routes
5 20 GUID_5_Routes
6 55 GUID_6_Routes
7 55 GUID_7_Routes
8 55 GUID_8_Routes
9 1 GUID_9_Routes
10 1 GUID_10_Routes
如您所见,GUID 是唯一的,ItemID 可以自行重复。 每个 GUID 都有一个路由列表,所有路由列表至少有 1 个条目及以上。
路由是 ItemList 类的一部分 public List<Waypoint> Routes = new List<Waypoint>();
路由示例。
GUID_1_Routes 有:
Entry Zone SubID Heading PosX PosY PosZ
1 1200 0 100 1029.32 837.21 29.10
2 1200 0 120 1129.32 537.21 29.10
3 1200 0 180 1229.32 137.21 29.10
4 1200 0 360 1329.32 437.21 29.10
5 1200 0 100 1429.32 637.21 29.10
GUID_2_Routes 有:
Entry Zone SubID Heading PosX PosY PosZ
1 100 0 10 129.32 437.21 29.10
所以我想做的是列出我在 myList 上的所有条目,这些条目按 ItemID 分组,维护字段 ItemID 和 Name ...以及每个 ItemID 的新列表,它将存储来自每个 GUID 的每个 Routes 的第一个元素具有相同的 ItemID。
例如 ItemID 20 会产生以下结果:
ItemID, Name, ListOfRoutes
这个 ItemID ListOfRoutes 将包含
GUID_1_Routes 第一个条目:
Entry Zone SubID Heading PosX PosY PosZ
1 1200 0 100 1029.32 837.21 29.10
GUID_2_Routes 第一个条目:
Entry Zone SubID Heading PosX PosY PosZ
1 100 0 10 129.32 437.21 29.10
GUID_3_Routes、GUID_4_Routes、GUID_5_Routes 第一个条目。
更新 2:
这就是我设法获得我想要的结果的方式,但我仍然相信它可以通过单个查询或以比我目前使用的更好的方式实现:
var query = from ItemList item in myList
where status.Contains(item.Status)
group item by new
{
item.ItemID,
item.Name,
item.Zone
}
into newList
orderby newList.Key.ItemID ascending
select new
{
newList.Key.ItemID,
newList.Key.Name,
newList.Key.Zone
};
foreach (var thisItem in query)
{
var location = from n in myList
where n.ItemID == thisItem.ItemID
select new
{
Routes = n.Routes.First()
};
foreach (var thisObject in location)
{
ItemList.Waypoint thisRoutes = thisObject.Routes;
}
}
请您参考如下方法:
所以您正在寻找的是我在第一个查询中所拥有的变体。我仍然不能 100% 确定我理解了你的解释,但根据你的输出,我相信这更多是你想做的。
var query = from entry in myList
where status.Contains(entry.Status)
group entry.Routes.First() // take the first item in each route
by new // assuming each id has a unique name
{
entry.ItemID,
entry.Name
}
into g
select new
{
g.Key.ItemID,
g.Key.Name,
ListOfRoutes = g.ToList() // return the grouping as list
};
我不确定您想要列表的类型,请根据需要进行更改。
