• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

Linq Where clause

Hello EE,

Lets say i have :

        Dim myMatch = From combo In (From _set In DataSpreadsResults Select _set)
                      Where combo.Value > 0
                      Select combo

now If I read myMatch lets say it returns results like:
1, 100
4, 500             (like a dimensional array)

my question is....
lets say I have another dimensional array that as the same indexes (1 and 4) but different values in it

how can I write another LINQ so in the where clause I could do something like :

        Dim mySum = From combo In (From _set In DataSpreads Select _set)
                    Where combo.Index   "INSIDE" (myMatch.Indexes)
 

do you understand what I mean ?  

i kind of SQL  Where IN ( 1,2,3,4)


in addition to all that.. I need the Sum of the results...    I dont think I can do   select combo.sum()



0
PhilippeRenaud
Asked:
PhilippeRenaud
  • 10
  • 8
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I know the structure of DataMatch().  What is the name and structure of the other class?  What are they being held in?
0
 
PhilippeRenaudAuthor Commented:
so at the end DataSpread looks like:


0, 100
1, 200
2, 700
3, 340
4, 1000
5, 780

etc


DataSpreadResults (at the end) looks like:

0, 0
1, 0
2, 200
3, 340
4, 0
5, 0
etc



For i As Integer = 0 To myValues.Count - 1
            Dim D As New APMatchingData
            Dim R As New APMatchingData

            D.Index = i
            D.Value = myValues(i)

            R.Index = i
            R.Value = 0

            DataSpreads.Add(D)
            DataSpreadsResults.Add(R)
        Next


.....

here I do some code and I modify some element value (not the index) inside DataSpreadsResults

Open in new window

0
 
PhilippeRenaudAuthor Commented:
in other words, I would need the sum of the index 2 and 3 (from my last code snippet) not of the value 200 and 340  but form the value 700 and 340 from DataSpread

thats why I was saying.. WHERE index in (2,3)....
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hmmm....not following exactly what you were doing with index 2 and 3 in your last posts.

It seems like you want to combine the two sets, linked by their Index() fields, and with the Value() field the sum of the two different Value()s?
0
 
PhilippeRenaudAuthor Commented:
I think you have it right Idle_Mind.

To repeat in my words,

Let me write it as a sql query so you'll understand perfectly:

I would to the following:

Select sum(ds.values)
From DataSpreadResults ds
WHERE ds.Index IN (2,3)


or i could do:

Select sum(ds.values)
From DataSpreadResults ds
WHERE ds.Index IN (SELECT index from DataSpread)


so in linq... im quite lost not sure i can do a Where IN .. or things like that..   if its still confusing... I will answer yes to your last question and I will try from there..
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try something like this:
Dim combined = From set1 In DataSpreads
                       Join set2 In DataSpreadsResults
                       On set1.Index Equals set2.Index
                       Select Index = set1.Index, Sum = set1.Value + set2.Value

        For Each match In combined
            Debug.Print(match.Index & " = " & match.Sum)
        Next

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
This is what I tested with:  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DataSpreads As New List(Of APMatchingData)
        Dim DataSpreadsResults As New List(Of APMatchingData)

        DataSpreads.AddRange(New APMatchingData() {New APMatchingData() With {.Index = 0, .Value = 100}, _
                                                   New APMatchingData() With {.Index = 1, .Value = 200}, _
                                                   New APMatchingData() With {.Index = 2, .Value = 700}, _
                                                   New APMatchingData() With {.Index = 3, .Value = 340}, _
                                                   New APMatchingData() With {.Index = 4, .Value = 1000}, _
                                                   New APMatchingData() With {.Index = 5, .Value = 780} _
                                                   })

        DataSpreadsResults.AddRange(New APMatchingData() {New APMatchingData() With {.Index = 0, .Value = 0}, _
                                                          New APMatchingData() With {.Index = 1, .Value = 0}, _
                                                          New APMatchingData() With {.Index = 2, .Value = 200}, _
                                                          New APMatchingData() With {.Index = 3, .Value = 340}, _
                                                          New APMatchingData() With {.Index = 4, .Value = 0}, _
                                                          New APMatchingData() With {.Index = 5, .Value = 0} _
                                                          })

        Dim combined = From set1 In DataSpreads
                       Join set2 In DataSpreadsResults
                       On set1.Index Equals set2.Index
                       Select Index = set1.Index, Sum = set1.Value + set2.Value

        For Each match In combined
            Debug.Print(match.Index & " = " & match.Sum)
        Next
    End Sub

Open in new window


It's output was:
0 = 100
1 = 200
2 = 900
3 = 680
4 = 1000
5 = 780
0
 
PhilippeRenaudAuthor Commented:
Yup, that's what I was missing....!

Wasn't sure about the join.

thanks idle
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I think it's also possible using a "where" instead of a "join"...would have to play with the syntax.
0
 
PhilippeRenaudAuthor Commented:
Yea..I guess its possible. but the join is enough straight-forward
!
0
 
PhilippeRenaudAuthor Commented:
Idle_Mind ever considered doing iPhone Apps? ( question like that :p )
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Never done mobile apps before.  What language(s) are most iPhone apps done in?  I'm guessing it's not .Net!  =)
0
 
PhilippeRenaudAuthor Commented:
it is called Objective-C, it is highly object oriented, quite nice to play with..

The best environment would be XCode via a Mac (ex. MacBook Pro)
0
 
PhilippeRenaudAuthor Commented:
how come :

For each match in combined ..


at match I receive error saying not declared... ?

if I put  as Object it works..  but I do not see intellisense on match.      (even if it works as match.Sum)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"match" is a dynamic, anonymous type which should not need to be declared as it will be inferred from what is contained within the "combined" variable.

Can you show me more code?...perhaps we can spot the problem.
0
 
PhilippeRenaudAuthor Commented:
its the same thing as you... or almost
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim DataSpreads As New List(Of APMatchingData)
        Dim DataSpreadsResults As New List(Of APMatchingData)

        DataSpreads.AddRange(New APMatchingData() {New APMatchingData() With {.Index = 0, .Value = 100}, _
                                                   New APMatchingData() With {.Index = 1, .Value = 200}, _
                                                   New APMatchingData() With {.Index = 2, .Value = 700}, _
                                                   New APMatchingData() With {.Index = 3, .Value = 340}, _
                                                   New APMatchingData() With {.Index = 4, .Value = 1000}, _
                                                   New APMatchingData() With {.Index = 5, .Value = 780} _
                                                   })

        DataSpreadsResults.AddRange(New APMatchingData() {New APMatchingData() With {.Index = 2, .Value = 200}, _
                                                          New APMatchingData() With {.Index = 3, .Value = 340}
                                                          })

        Dim combined = From set1 In DataSpreads
                       Join set2 In DataSpreadsResults
                       On set1.Index Equals set2.Index
                       Select set1.Index, Sum = set1.Value + set2.Value
        Dim mySum As Decimal

        For Each match In combined
            mySum += match.Sum
        Next

        Console.WriteLine(mySum)
    End Sub

Open in new window

0
 
PhilippeRenaudAuthor Commented:
Error      18      'match' is not declared. It may be inaccessible due to its protection level
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It works perfectly for me...even with both Option Explicit and Option Strict turned on.

Do you by chance have a variable called "match" declared OUTSIDE the button click handler causing problems?

Here's my test project in its entirety: (VS2010 targeting 4.0)
Option Strict On
Option Explicit On
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DataSpreads As New List(Of APMatchingData)
        Dim DataSpreadsResults As New List(Of APMatchingData)

        DataSpreads.AddRange(New APMatchingData() {New APMatchingData() With {.Index = 0, .Value = 100}, _
                                                   New APMatchingData() With {.Index = 1, .Value = 200}, _
                                                   New APMatchingData() With {.Index = 2, .Value = 700}, _
                                                   New APMatchingData() With {.Index = 3, .Value = 340}, _
                                                   New APMatchingData() With {.Index = 4, .Value = 1000}, _
                                                   New APMatchingData() With {.Index = 5, .Value = 780} _
                                                   })

        DataSpreadsResults.AddRange(New APMatchingData() {New APMatchingData() With {.Index = 2, .Value = 200}, _
                                                          New APMatchingData() With {.Index = 3, .Value = 340}
                                                          })

        Dim combined = From set1 In DataSpreads
                       Join set2 In DataSpreadsResults
                       On set1.Index Equals set2.Index
                       Select set1.Index, Sum = set1.Value + set2.Value
        Dim mySum As Decimal

        For Each match In combined
            mySum += match.Sum
        Next

        Console.WriteLine(mySum)
    End Sub

End Class

Public Class APMatchingData
    Public Index As Integer
    Public Value As Decimal
End Class

Open in new window

0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now