Quering container with Linq + group by ?

Posted by Prix on Stack Overflow See other posts from Stack Overflow or by Prix
Published on 2011-01-01T02:55:41Z Indexed on 2011/01/01 3:54 UTC
Read the original article Hit count: 365

Filed under:
|
|
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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ