Results 1 to 7 of 7

Thread: [RESOLVED] Linq Query Question

  1. #1

    Thread Starter
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Resolved [RESOLVED] Linq Query Question

    Hey, guys. So I'm still learning LINQ here, and am having an issue with what should be an easy query.

    Basically, my query is this: I want all of the characters that are of a membership level in a given area that is of admin or above.

    This is what I have so far:

    Code:
    Dim L As List(Of Character) = (From C In MasterCharacterList.Characters.Values _
        Where Me.Item(C.CharID).DreamLevel.ContainsValue( _
            (From L In _Levels Where L.Value.Admin Or L.Value.BotMaster Select L.Value.MemberLevel).ToList.ForEach) Select C)
    Me, in this case, is the members collection.

    So far, I can't get the query to turn into a list (hence no .tolist at the end.) I know there's something wrong here, but I'm not sure what.
    Last edited by Campion; May 12th, 2009 at 03:22 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Linq Query Question

    Well, my first advice is to format your code so you can see what's going on. It's very hard to read that code.
    vb.net Code:
    1. Dim L As List(Of Character) = (From C In MasterCharacterList.Characters.Values _
    2.                                Where Me.Item(C.CharID).DreamLevel.ContainsValue((From L In _Levels _
    3.                                                                                  Where L.Value.Admin Or L.Value.BotMaster _
    4.                                                                                  Select L.Value.MemberLevel).ToList.ForEach) _
    5.                                Select C)
    Ahhhhh. Isn't that better? Now you can actually see the intent.

    As for the question, I think we will need more information about the types involved. What are the relationships? What's a DreamLevel? Etc,. etc.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Linq Query Question

    I'm suspecting the problem is here:
    Code:
    (From L In _Levels _
    Where L.Value.Admin Or L.Value.BotMaster _
    Select L.Value.MemberLevel).ToList.ForEach)
    But don't know for sure. Do they work independently? Can you do something like:
    Code:
    Dim myL As List(Of Levels) = (From L In _Levels _
                                 Where L.Value.Admin Or L.Value.BotMaster _
                                 Select L.Value.MemberLevel).ToList
    Dim L As List(Of Character) = (From C In MasterCharacterList.Characters.Values _
                                  Where Me.Item(C.CharID).DreamLevel.ContainsValue(myL.ForEach) _
                                  Select C)
    Does myL produce a proper list?
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  4. #4

    Thread Starter
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Linq Query Question

    Sorry, JL. The indentions don't always copy over correctly.

    Dreamlevel is a collection that contains a member's membership level in a certain area (dream.) The number this is the key to the memberlevel for what he can and cannot do.

    Jenner, I won't be able to test out what you've suggested until tomorrow. Work is until O'dark 30 tonight. But I will try it.

  5. #5

    Thread Starter
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Linq Query Question

    Well, I compromised a little bit and did half LINQ, and half FOR EACH, but it still achieves the same result. However, I'd still like to figure out how to do it all in LINQ eventually.

    Code:
    Private Function GetAdmin() As List(Of Character)
    
            'return variable
            Dim Li As New List(Of Character)
    
            'Grab the list of memberlevels that hold admin rank or above
            Dim Le As List(Of Integer) = (From L In _Levels Where L.Value.Admin Or _
                                L.Value.BotMaster Select L.Value.MemberLevel).ToList
    
            'Here, we compare the list of admin levels with what a member actually has. If he has an
            'admin level or above in any dream in the continuity, he will be included on the list,
            ' regardless of whether or not he may be able to access that dream to begin with.
    
            For Each M As Member In MasterCharacterList.Members
                For i As Integer = 0 To Le.Count - 1
                    If M.DreamLevel.ContainsValue(Le.Item(i)) Then
                        Dim C As Character = MasterCharacterList.Characters.Item(M.ID)
                        If Not Li.Contains(C) Then
                            Li.Add(C)
                        End If
                    End If
                Next
            Next
    
            Return Li
    
        End Function
    I'll set this for resolved, but any input would be greatly appreciated.

  6. #6
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [RESOLVED] Linq Query Question

    I think I got what you're looking for, you're trying to compare one list vs. another list and if ANY items from list 2 are found in list one, then flag that character?

    How about using "Intersect" to get the items that appear on both lists:

    Code:
            Dim Le As List(Of Integer) = (From L In _Levels _
                                         Where L.Value.Admin Or L.Value.BotMaster _
                                         Select L.Value.MemberLevel).ToList
            Dim Lc As List(Of Character) = (From C As Character In MasterCharacterList.Characters.Values _
                                          Where Me.item(C.CharID).DreamLevel.Intersect(Le) _
                                          Select C).ToList
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  7. #7

    Thread Starter
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: [RESOLVED] Linq Query Question

    Intersect is expecting a KVP(of string, integer), but I'm not sure how to get that when the key for_Levels in an integer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width