linq any function vb.net

1.2k Views Asked by At

I have this code and it works as excepted:

            For i = 0 To _SelectedFields.Count - 1
                For j = 0 To _SelectedFields(i).DataFields.Count - 1
                    For k = 0 To _Aggregations.Count - 1
                        If Not _Aggregations(k).AggregatedExpr.Contains(_SelectedFields(i).DataFields(j).FieldName) Then
                            If Not FieldInGroupBy.Contains(_SelectedFields(i).DataFields(j).FieldName) Then
                                MessageBox.Show("Missing selected field '" & _SelectedFields(i).DataFields(j).FieldName & "' at GROUP BY.", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
                                Exit Sub
                            End If
                        End If
                    Next
                Next
            Next

I though, I make it with linq:

    If Not _SelectedFields.Any(Function(SelectedField) SelectedField.DataFields.Any(Function(DataField)
                                                                                        If Not _Aggregations.Any(Function(Aggregation) Aggregation.AggregatedExpr.Contains(DataField.FieldName)) Then
                                                                                            Return FieldInGroupBy.Contains(DataField.FieldName)
                                                                                        Else
                                                                                            Return True
                                                                                        End If
                                                                                    End Function)) Then
        MsgBox("error")
    End If

In my opinion, the above is the same as the working one. But it doesn't work as intended.

Have you any ideas, why this doesn't work?

1

There are 1 best solutions below

0
On BEST ANSWER

If your first bit of code is your intended behaviour then I think this suffices:

Dim query = _
    From sf In _SelectedFields _
    From df In sf.DataFields _
    From ag In _Aggregations _
    Where Not ag.AggregatedExpr.Contains(df.FieldName) _
    Where Not FieldInGroupBy.Contains(df.FieldName) _
    Select df.FieldName

For Each x in query
    MessageBox.Show( _
        "Missing selected field '" & x & "' at GROUP BY.", _
        "", _
        MessageBoxButtons.OK, MessageBoxIcon.Error)
    Exit Sub
Next