Link to home
Start Free TrialLog in
Avatar of Philippe Renaud
Philippe RenaudFlag for Canada

asked on

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()



Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

I know the structure of DataMatch().  What is the name and structure of the other class?  What are they being held in?
Avatar of Philippe Renaud

ASKER

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

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)....
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?
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..
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

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yup, that's what I was missing....!

Wasn't sure about the join.

thanks idle
I think it's also possible using a "where" instead of a "join"...would have to play with the syntax.
Yea..I guess its possible. but the join is enough straight-forward
!
Idle_Mind ever considered doing iPhone Apps? ( question like that :p )
Never done mobile apps before.  What language(s) are most iPhone apps done in?  I'm guessing it's not .Net!  =)
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)
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)
"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.
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

Error      18      'match' is not declared. It may be inaccessible due to its protection level
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