Queyring container with Linq + group by ?
- by Prix
public class ItemList
{
public int GuID { get; set; }
public int ItemID { get; set; }
public string Name { get; set; }
public entityType Status { get; set; }
public class Waypoint
{
public int Zone { get; set; }
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>();
}
I have a list of items using the above class and now I need to group it by ItemID and join the first entry of Routes of each iqual ItemID.
So for example, let's say on my list I have:
GUID ItemID ListOfRoutes
1 23 first entry only
2 23 first entry only
3 23 first entry only
4 23 first entry only
5 23 first entry only
6 23 first entry only
7 23 first entry only
Means I have to group entries 1 to 7 as 1 Item with all the Routes entries.
So I would have one ItemID 23 with 7 Routes on it where those routes are the first element of that given GUID Routes List.
My question is if it is possible using LINQ to make a statment to do something like that this:
var query = from ItemList entry in myList
where status.Contains(entry.Status)
group entry by entry.ItemID into result
select new
{
items = new
{
ID = entry.ItemID,
Name = entry.Name
},
routes = from ItemList m in entry
group m.Routes.FirstOrDefault() by n.NpcID into m2
};
So basicly I would have list of unique IDS information with a inner list of all the first entry of each GUID route that had the same ItemID.
<<< UPDATE:
This would be an example of public List<ItemList> myList = new List<ItemList>(); data:
GUID ItemID ListOfRoutes
1 20 Routes[1]
2 20 Routes[2]
3 20 Routes[3]
4 20 Routes[4]
5 20 Routes[5]
6 55 Routes[6]
7 55 Routes[7]
8 55 Routes[8]
9 1 Routes[9]
10 1 Routes[10]
As you can see GUID is unique, ItemID can reapeat it self.
Each GUID has a Routes list and all routes list have a minimum of 1 entry and above.
Routes example.
Routes[1] have:
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
Routes[2] have:
Entry Zone SubID Heading PosX PosY PosZ
1 100 0 10 129.32 437.21 29.10
So what I want to do is a list of all entries I have on myList grouped by ItemID maintainning the fields ItemID and Name ... and a new field or item that will have all the first elements of Routes of those GUIDs.
For example ItemID 20 would produce the follow result:
ItemID, Name, ListOfRoutes
This ItemID ListOfRoutes would contain
Routes[1] first entry:
Entry Zone SubID Heading PosX PosY PosZ
1 1200 0 100 1029.32 837.21 29.10
Routes[2] first entry:
Entry Zone SubID Heading PosX PosY PosZ
1 100 0 10 129.32 437.21 29.10
Routes[3], Routes[4], Routes[5] first entries.
Example of how myList is feeded:
ItemList newItem = new ItemList();
newItem.GUID = GUID;
newItem.ItemID = ItemID;
newItem.Name = Name;
newItem.Status = Status;
// Item location
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);