使用 Linq 查询容器分组依据?

发布于 2024-10-10 05:54:45 字数 4331 浏览 0 评论 0原文

我有这个类:

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>();
}

在此列表中使用它: 公共列表 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的示例。 myList = new List(); data:

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的一部分。 Routes = new List();

路线示例。

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 的新列表,该列表将存储具有相同 ItemID 的每个 GUID 中每个路由的第一个元素。

例如,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;
                }
        }

I have this class:

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>();
}

Which is used on this List:
public List myList = new List();

New items are added to is like this:

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);

Now I need to group it by ItemID and join the first entry of Routes of each iqual ItemID.

This would be an example of public List<ItemList> myList = new List<ItemList>(); data:

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

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 is part of the class ItemList public List<Waypoint> Routes = new List<Waypoint>();

Routes example.

GUID_1_Routes 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

GUID_2_Routes 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 list per ItemID that will store the first element of each Routes from each GUID that had the same ItemID.

For example ItemID 20 would produce the follow result:

ItemID, Name, ListOfRoutes

This ItemID ListOfRoutes would contain

GUID_1_Routes first entry:

Entry   Zone    SubID   Heading     PosX    PosY    PosZ
    1   1200        0       100     1029.32 837.21  29.10

GUID_2_Routes first entry:

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 first entries.

UPDATE2:

This is how I have managed to get the results I wanted but I still belive it would be possible with a single query or in a better way then what I am currently using:

        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;
                }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意犹 2024-10-17 05:54:45

因此,您正在寻找的是我在第一个查询中找到的内容的变体。我仍然不能百分百确定我理解了你的解释,但根据你的输出,我相信这更多的是你想要做的事情。

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
            };

我不确定您想要的列表类型,请根据需要进行更改。

So what you're looking for is a variant of what I had in the first query. I'm still not 100% sure I understood your explanation but based on your output, I believe this is more of what you were trying to do.

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
            };

I wasn't sure about what type you wanted for the list, change it as necessary.

断爱 2024-10-17 05:54:45
  • 在您的问题中,您说:“来自具有相同 ItemID 的每个 GUID。”
  • 在您的工作代码中,您根本不使用 GUID。

-

  • 在您的问题中,您没有提到状态过滤。
  • 在您的工作代码中,有状态过滤。

-

不管怎样,我已经将“工作代码”简化为几行。

from ItemList item in myList
where status.Contains(item.status)
group item by item.ItemID into groupedItems
order by groupedItems.Key descending
let firstItem = groupedItems.First()
let routes =
  from item2 in groupedItems
  select item2.Routes.First()
from route in routes
select new
{
  TheItemList = firstItem,
  TheWayPoint = route
};

您需要学习如何查询group by操作的结果。在上面的代码中,我从每个组中提取了第一个元素,并且投影了该组的每个元素,然后加入该投影。

您还应该更好地传达每个键标识的内容、为什么列表中首先存在重复的项目 ID,以及为什么名为 ItemList 的类根本不是列表。

  • In your question, you say: "from each GUID that had the same ItemID."
  • In your working code, you do not use GUID at all.

-

  • In your question, you do not mention status filtering.
  • In your working code, there is status filtering.

-

Anyway, I've boiled down the "working code" to a few lines.

from ItemList item in myList
where status.Contains(item.status)
group item by item.ItemID into groupedItems
order by groupedItems.Key descending
let firstItem = groupedItems.First()
let routes =
  from item2 in groupedItems
  select item2.Routes.First()
from route in routes
select new
{
  TheItemList = firstItem,
  TheWayPoint = route
};

You need to learn how to query the result of the group by operation. In the above code, I have extracted the first element from each group, and I have projected each element of the group, then joined on that projection.

You should also communicate better what each key identifies, why there are duplicate item IDs in the list to begin with, and why a class named ItemList is not a List at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文