3

I have Window Application in VB.Net. I have following Classes & Enum.

Public Class Page
    Public Property Name As String
    Public Property PageItems As List(Of PageItem)
End Class


Public Class PageItem
    Public Property ItemName As String
    Public Property mode As Mode
End Class

Public Enum Mode
    Mode1
    Mode2
End Enum

In my application I have a List(Of Page) property. One Page may have multiple PageItemand also PageItem.ItemName can be duplicated with different modes. e.g. Application can have a PageItem with Mode1 and Mode2 (Here it's possible that Mode1 item may in Page(0) and Mode2 item may in Page(1))

My Question is How to find a PageItems List (Same ItemName) which have both Mode1 and Mode2 from a List(Of Page) property using LINQ ?

Example:

Page 1 has 3 Items {Item1, Mode1}, {Item2,Mode1}, {Item3,Mode2}

Page 2 has 2 Items {Item1, Mode2}, {Item4, Mode1}

Here I want a PageItems list which have both mode Mode1 & Mode2. In the above case the result should be {Item1, Mode1}, {Item1, Mode2} (Item1 have both Modes)

4

3 回答 3

1

当您需要“PageItems 组”时,我采用了这种方法:

From p In pages From pi In p.PageItems _
 Group By pi.ItemName Into Group _
 Where (New Mode(){Mode.Mode1, Mode.Mode2}).All(Function(m) _
               (From pig In Group Where pig.pi.Mode = m).Any) _
 Select ItemName, PageItems=(From pig In Group Select pig.pi)

注意最后Select是减少可用信息。如果你删除它,你也会得到每个组的页面。

为了测试这一点,我使用了你的数据集,除了我有Item3Mode2而不是在枚举中添加新的),现在在我将评论更改为Mode3的评论中测试你的问题的答案,以便为我的上面返回两组回答。Item4Item3

“只得到一个”的答案只是对最后一行List(Of PageItems)的更改:Select

From p In pages From pi In p.PageItems _
 Group By pi.ItemName Into Group _
 Where (New Mode(){Mode.Mode1, Mode.Mode2}).All(Function(m) _
               (From pig In Group Where pig.pi.Mode = m).Any) _
 From pig In Group Select pig.pi
于 2012-09-17T01:08:52.680 回答
0

如果我理解正确,认为应该这样做。它应该返回包含 mode1 和 mode2 类型的页面项的所有页面。你试过什么了?

from p in pages
where p.PageItems.Any(i => i.mode == Mode1) && p.PageItems.Any(i => i.mode == Mode2)
select p
于 2012-09-14T10:18:59.627 回答
0

你可以试试下面的代码。

将页面变暗为列表(页面)=新列表(页面)()

    Dim page1 As Page = New Page()
    page1.Name = "Page1"
    page1.PageItems = New List(Of PageItem)()

    Dim pageItem1 = New PageItem()
    pageItem1.ItemName = "Item1"
    pageItem1.mode = Mode.Mode1

    page1.PageItems.Add(pageItem1)

    Dim pageItem2 = New PageItem()
    pageItem2.ItemName = "Item2"
    pageItem2.mode = Mode.Mode1

        page1.PageItems.Add(pageItem2)

    Dim pageItem3 = New PageItem()
    pageItem3.ItemName = "Item3"
    pageItem3.mode = Mode.Mode2

    page1.PageItems.Add(pageItem3)

    pages.Add(page1)

    Dim page2 As Page = New Page()
    page2.Name = "Page2"
    page2.PageItems = New List(Of PageItem)()

    Dim pageItem4 = New PageItem()
    pageItem4.ItemName = "Item1"
    pageItem4.mode = Mode.Mode2

    page2.PageItems.Add(pageItem4)

    Dim pageItem5 = New PageItem()
    pageItem5.ItemName = "Item4"
    pageItem5.mode = Mode.Mode1

    page2.PageItems.Add(pageItem5)

    pages.Add(page2)

    Dim ps = From p In pages From pi In p.PageItems Group pi By pi.ItemName Into gp = Group Where gp.Count() > 1 From pil In gp Select pil

Page、PageItem 和 Mode 类取自您的示例代码。最后一行将为您提供所需的结果。

于 2012-09-18T15:10:01.323 回答