【问题标题】:LINQ query is not workingLINQ 查询不起作用
【发布时间】:2017-02-24 17:30:01
【问题描述】:

我正在尝试从列表中创建字典。我正在尝试过滤列表,使其包含我作为键添加到字典中的 id。所以字典将是键,列表 - 以 id 作为键。
我不知道为什么以下内容不起作用 - 尽管 rawlist 包含 ids,但过滤列表是空的。我正在检查循环中的值。请给我一双新鲜的眼睛? resultList 是字典(字符串,列表)

    For Each c As String In listIds
    Dim filteredlist = rawList.Where(Function(x) x.id.Trim().Contains(c.Trim())).ToList()
    resultList.Add(c,filteredlist)
    Next

我需要过滤列表。我试过包含、等于和“=” 即

    Dim filteredlist = rawList.Where(Function(x) x.id.Trim().Equals(c.Trim())).ToList()

    Dim filteredlist = rawList.Where(Function(x) x.id.Trim() = (c.Trim())).ToList()

请查收,谢谢!

【问题讨论】:

  • “我需要过滤列表。” 毫无疑问。代码在什么意义上“不起作用”? rawlist 包含 ID,同时是空的?你能描述一下你看到什么,以及你看到了什么吗?
  • 对不起。我虽然 rawlist 包含 id = c 的项目,但过滤后的列表是空的。
  • 一旦你应用了过滤器,你就不会对filteredList 做任何事情。如果您需要更深入的帮助,则需要提供有关输入、预期输出和实际输出的更多详细信息。
  • id 等于c 时,如果您真的想要id 是否包含c,有什么特别的原因吗?
  • 你在哪里看filteredList?在循环内部还是外部?

标签: vb.net linq lambda


【解决方案1】:

看起来你有它倒退。通常,您有一个列表用于首先查找某些内容,而不是遍历该列表,恕我直言,这需要更长的时间。

Filter Linq Child Collection by another List/Array

Public Class POC
    Public Property Id As Integer
    Public Property Desc As String
    Public Property Orders As List(Of Order)
End Class

Public Class Order
    Public Property Id As Integer
    Public Property Desc As String
End Class

Private Shared Function GetOrders(numberOfOrders As Integer) As List(Of Order)
    Dim orders = New List(Of Order)()

    For i As Integer = 1 To numberOfOrders
        orders.Add(New Order() With { .Id = i, .Desc = "{i} Order" })
    Next

    Return orders
End Function

Private Shared Function GetPOCOsAndOrders() As List(Of POC)
    Return New List(Of POC)() From { _
        New POC() With { .Id = 1, .Desc = "John", .Orders = GetOrders(1) }, 
        New POC() With { .Id = 2, .Desc = "Jane", .Orders = GetOrders(2) },
        New POC() With { .Id = 3, .Desc = "Joey", .Orders = GetOrders(3) }
End Function


Private Shared Sub Main(args As String())
    Dim orders = New List(Of Integer)() From { 2, 3 }
    Dim items = GetPOCOsAndOrders()

    Dim peopleAndOrdersWhereOrderNumberIsGreaterThanTwo = items.Where(Function(x) x.Orders.Any(Function(y) orders.Contains(y.Id)))

    'I should only get the last two people out of three and their orders
    peopleAndOrdersWhereOrderNumberIsGreaterThanTwo.ToList().ForEach(Function(x) Console.WriteLine("{x.Id} {x.Desc} {x.Orders.Count}"))

    Console.ReadLine()
End Sub

【讨论】:

    【解决方案2】:

    由于您提供给我们的信息有限,可能会有许多细微的变化,但一个可行的例子是:

    Option Infer On
    Option Strict On
    
    Module Module1
    
        Public Class datum
            Property Id As String
            Property Name As String
    
            Public Sub New()
                ' empty constructor
            End Sub
    
            Public Sub New(Id As String, Name As String)
                Me.Id = Id
                Me.Name = Name
            End Sub
    
        End Class
    
        Sub Main()
            ' sample data...
            Dim rawData = New List(Of datum)
            rawData.Add(New datum("cat", "Lulu"))
            rawData.Add(New datum("cat", "Uschi"))
            rawData.Add(New datum("snake", "Sid"))
            rawData.Add(New datum("fox", "Reynard"))
            rawData.Add(New datum("mouse", "Jerry"))
    
            ' what to look for:
            Dim listIds As New List(Of String) From {"CAT", "mouse", "ELK"}
    
            ' somewhere to store the results:
            Dim dict As New Dictionary(Of String, List(Of String))
    
            ' filter by what to look for:
            For Each c In listIds
                Dim foundItems = rawData.Where(Function(x) String.Compare(x.Id, c, StringComparison.OrdinalIgnoreCase) = 0)
    
                ' only add items to the dictionary if the Id was found:
                If foundItems.Count() > 0 Then
                    ' use the case of the id from the raw data:
                    dict.Add(foundItems(0).Id, foundItems.Select(Function(f) f.Name).ToList())
                End If
    
                ' alternative which includes dictionary entries where the Id was not found:
                'dict.Add(If(foundItems.Count = 0, c, foundItems(0).Id), foundItems.Select(Function(f) f.Name).ToList())
    
            Next
    
            ' show the dictionary contents:
            For Each kvp In dict
                Console.WriteLine(kvp.Key & ": " & String.Join(", ", kvp.Value))
            Next
    
            Console.ReadLine()
    
        End Sub
    
    End Module
    

    输出:

    猫:露露,乌斯基
    鼠标:杰瑞

    我已经为您可能需要的一些变体添加了注释掉的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多