用于数组中封装值的Linq语法

时间:2013-04-03 12:00:18

标签: .net sql vb.net linq

我正在寻找任何LINQ语法来获取数据库频率范围的结果。

SimpleSet包含一个frequencySets数组。在每个FrequencySet中,它可以是几个频率。如果一个频率匹配最小值和最大值(1000 <2000),它可以保留在结果列表中。

当前代码:

    Dim ret As New ObservableCollection(Of fSimpleItemSet)'the result

    Dim FreqMin As Double = 1000
    Dim FreqMax As Double = 2000
    Dim retTmp As New ObservableCollection(Of fSimpleItemSet)' fSimpleSet is the data collection
    retTmp = (From fSimple As fSimpleItemSet In Data Select fSimple) 'Get ALL items

    For Each fSimple As fSimpleItemSet In retTmp
        If fSimple.fFrequencySet IsNot Nothing Then
            For Each fFrequency As fFrequencySet In fSimple.fFrequencySet
                If fFrequency.Frequency > FreqMin And fFrequency.Frequency < FreqMax Then
                    ret.Add(fSimple)
                    Exit For
                End If
            Next
        End If
    Next

Database structure

1 个答案:

答案 0 :(得分:1)

您可以使用Any方法查找至少有一个子项通过指定条件的元素:

Dim query = retTmp.Where(Function(fSimple) Not fSimple.fFrequencySet Is Nothing)
                  .Where(Function(fSimple) fSimple.fFrequencySet.Any(Function(fFrequency) fFrequency.Frequency > FreqMin And fFrequency.Frequency < FreqMax))

Dim retTmp As New ObservableCollection(Of fSimpleItemSet)(query)

或在一个Where电话中:

Dim query = retTmp.Where(Function(fSimple) Not fSimple.fFrequencySet Is Nothing AndAlso fSimple.fFrequencySet.Any(Function(fFrequency) fFrequency.Frequency > FreqMin And fFrequency.Frequency < FreqMax))