-
Apr 3rd, 2013, 06:01 PM
#1
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
-
Apr 4th, 2013, 06:36 AM
#2
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??
-
Apr 4th, 2013, 08:42 AM
#3
Re: VB.NET Twisted experiment with LINQ
Originally Posted by szlamany
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.
-
Apr 9th, 2013, 07:18 AM
#4
Re: VB.NET Twisted experiment with LINQ
I take it this is the funky constant thing you're talking about ?:-
vbnet Code:
GroupBy(Function(f) 3 > 0)
I was like "what the..." when I saw that lol.
Originally Posted by szlamany
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.
-
Apr 10th, 2013, 12:59 PM
#5
Re: VB.NET Twisted experiment with LINQ
Originally Posted by Niya
I take it this is the funky constant thing you're talking about ?:-
vbnet Code:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|