Results 1 to 5 of 5

Thread: VB.NET Twisted experiment with LINQ

  1. #1

    Thread Starter
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    VB.NET Twisted experiment with LINQ

    First off, this is purely an experiment and over time will be forgotten but not the (as I see it) why to avoid this method.

    Over in another forum a person asked “how to get the sum for column 1 and a sum for column 2 in a DataGridView for the first three rows?” Having sometime to tinker with this my first thought was why not work this out from the DataSource yet the OP did not specify how the DataGridView was loaded and another thought popped into my head (of course it was not the simple way and far from practical), let’s try this with LINQ. The second thought after thinking LINQ is no doubt this is a situation no matter what this is possible yet not practical as conventional methods (pre LINQ) would be better and that working from the data source is best as a DataGridView is a control to view data but get statistics.

    After getting close to a viable solution, the IDE started acting up as in slowing down. Throughout the time coding there were failures until I read a post where (and they did not say where they got this from) the developer did a where condition that was “constant” and behold the failures turn into a resolution.

    With that said, feel free to download the VS2010 project and try it out. Pasted trying it out I would advise not using code such as this against a DataGridView as this was an exercise only along with using the funky constant in the where condition and lastly unless you have a decent understanding of LINQ it will surely bite you down the road.

    Visual Studio 2010 project on SkyDrive.

    Sadistic and twisted code
    Code:
    Public Function GetInformation(ByVal sender As DataGridView, ByVal TakeCount As Int32) As String
        Dim sb As New System.Text.StringBuilder
        Dim Results =
                (
                    From row In sender.Rows.Cast(Of DataGridViewRow) _
                    .Take(TakeCount) _
                    .GroupBy(Function(f) 3 > 0) _
                    .Select(Function(group) _
                        New With
                        {
                            .C1Sum = group.Sum(Function(y) CInt(y.Cells(0).Value)),
                            .C2Sum = group.Sum(Function(y) CInt(y.Cells(1).Value)),
                            .C1Max = group.Max(Function(y) CInt(y.Cells(0).Value)),
                            .C2Max = group.Max(Function(y) CInt(y.Cells(1).Value)),
                            .C1Min = group.Min(Function(y) CInt(y.Cells(0).Value)),
                            .C2Min = group.Min(Function(y) CInt(y.Cells(1).Value))
                        }
                    )
                ).ToList
        sb.AppendLine("Column 1")
        sb.AppendLine(String.Format("Sum: {0} Min: {1} Max: {2}",
                                    Results(0).C1Sum,
                                    Results(0).C1Min,
                                    Results(0).C1Max
                                    )
                                )
        sb.AppendLine("")
        sb.AppendLine("Column 2")
        sb.AppendLine(String.Format("Sum: {0} Min: {1} Max: {2}",
                                    Results(0).C2Sum,
                                    Results(0).C2Min,
                                    Results(0).C2Max
                                    )
                                )
        Return sb.ToString
    End Function

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: VB.NET Twisted experiment with LINQ

    LINQ - I don't use it - and I gotta say that I can't imagine wanting to code something that cryptic!

    What a crazy syntax!!

    Is it close enough to T-SQL that you can leverage that understanding - or is it inconsistent with T-SQL??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: VB.NET Twisted experiment with LINQ

    Quote Originally Posted by szlamany View Post
    LINQ - I don't use it - and I gotta say that I can't imagine wanting to code something that cryptic!

    What a crazy syntax!!
    Hopefully nobody uses this example which is the point.

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: VB.NET Twisted experiment with LINQ

    I take it this is the funky constant thing you're talking about ?:-
    vbnet Code:
    1. GroupBy(Function(f) 3 > 0)

    I was like "what the..." when I saw that lol.

    Quote Originally Posted by szlamany View Post
    I can't imagine wanting to code something that cryptic!

    What a crazy syntax!!
    It isn't really that cryptic. If anything, its tedious to conjure up and to type out. I'm with Kevin though, this shouldn't be used against a DataGridView.
    Last edited by Niya; Apr 9th, 2013 at 07:21 AM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5

    Thread Starter
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: VB.NET Twisted experiment with LINQ

    Quote Originally Posted by Niya View Post
    I take it this is the funky constant thing you're talking about ?:-
    vbnet Code:
    1. GroupBy(Function(f) 3 > 0)

    I was like "what the..." when I saw that lol.


    It isn't really that cryptic. If anything, its tedious to conjure up and to type out. I'm with Kevin though, this shouldn't be used against a DataGridView.
    The main reason I described this as cryptic is that 3 > 0 was randomly selected so if someone tried to figure out WT? is this and not try something else they would be wasting time that could be spent on better things like having a beer. In regards to tedious to type out, heck there were at least three attempts before this version (yeah I admit to wasting time).

    And I can not stress enough to refrain from using this, no different than keeping far far away from Chernobyl.

Tags for this Thread

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