Author Topic: (vent) Introduction to LINQ  (Read 1958 times)

0 Members and 1 Guest are viewing this topic.

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 8840
(vent) Introduction to LINQ
« on: January 13, 2016, 04:09:41 pm »
So I've been using my trial of PluralSight and watching some C# stuff. I am now absolutely convinced that the C# development team is either drunk or the language is an April fools joke gone bad.

Granted, I realize this is just an intro and the author said as much but...

Code - C#: [Select]
  1. namespace IntroToLINQ {
  2.    [TestClass]
  3.    public class IntroToLINQ {
  4.  
  5.        [TestMethod]
  6.        public void Scratchpad() {
  7.  
  8.            List<string> names = new List<string> {
  9.                "Rob, Friend",
  10.                "Holly, Family",
  11.                "This isn't a name",
  12.                "Macolm, Colleague",
  13.                "Tom, Family"
  14.            };
  15.  
  16.            Regex pattern = new Regex( "([^,]*), (.*)" );
  17.  
  18.            var query = from line in names
  19.                        let match = pattern.Match( line )
  20.                        where match.Success
  21.                        select new {
  22.                            Name = match.Groups[1].Value,
  23.                            Relationship = match.Groups[2].Value
  24.                        } into association
  25.                        group association.Name by association.Relationship;
  26.  
  27.            foreach( var group in query ) {
  28.                Console.WriteLine( "Relationship: {0}", group.Key );
  29.                foreach( var name in group ) {
  30.                    Console.WriteLine( "   {0}", name );
  31.                }
  32.            }
  33.        }
  34.    }
  35. }
  36.  
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

kdub

  • SuperMod
  • Swamp Rat
  • Posts: 978
  • class keyThumper<T>:ILazy<T>
Re: (vent) Introduction to LINQ
« Reply #1 on: January 13, 2016, 06:35:23 pm »

Where is your problem John ?

Syntax or mechanics ?

PS. which course ?



Relationship: Friend
   Rob
Relationship: Family
   Holly
   Tom
Relationship: Colleague
   Macolm

called Kerry in my other life

Sometimes the question is more important than the answer.

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 8840
Re: (vent) Introduction to LINQ
« Reply #2 on: January 13, 2016, 09:55:37 pm »
Mostly mechanics; the ability to mix in Sql statements is a bit of a much for me--added with that all the inferencing (compile/run time). I guess just feeling overwhelmed again (all the syntax tricks and shortcuts I'm picking up are making the language and my interpretation of those sugar laced calls feel under developed).

I'll get you the name in the morning but this was supposed to be a quick c# refresher for me. I essentially finished the course but I am going to go back and take notes before I say I am officially done. The course is by Jon Skeet and Rob Conery. My next course was going to be the advanced courses from them.

Side note: I'm also looking at Swift as well, and so far I like the language.
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

kdub

  • SuperMod
  • Swamp Rat
  • Posts: 978
  • class keyThumper<T>:ILazy<T>
Re: (vent) Introduction to LINQ
« Reply #3 on: January 13, 2016, 10:07:23 pm »

I assume that would be 

Exploring C# 6 with Jon Skeet which only mentions LINQ in passing.

There were some (several) LINQ courses by Scott Allen dated around 2008 which take us through from beginner to advanced.

John,
Did you know that LINQ can be used with success on the AutoCAD database ?

called Kerry in my other life

Sometimes the question is more important than the answer.

kdub

  • SuperMod
  • Swamp Rat
  • Posts: 978
  • class keyThumper<T>:ILazy<T>
Re: (vent) Introduction to LINQ
« Reply #4 on: January 13, 2016, 10:13:40 pm »

Side note: I'm also looking at Swift as well, and so far I like the language.

I can't make the time to look at iOS, OSX specific languages unfortunately.

... and my brain isn't what it used to be :-(
called Kerry in my other life

Sometimes the question is more important than the answer.

gile

  • Water Moccasin
  • Posts: 2089
  • Marseille, France
Re: (vent) Introduction to LINQ
« Reply #5 on: January 14, 2016, 02:50:19 am »
Hi,

Tell me if I'm off topic, but I really like these drunk guys who brought some functional programming features in C# with Linq.
By my side, I use them a lot when dealing with collections but I rather use the extension methods syntax with chaining which is closer to F# pipelining than SQL queries.

C#
Code - C#: [Select]
  1. var query =
  2.    names
  3.    .Select(n => pattern.Match(n))
  4.    .Where(m => m.Success)
  5.    .Select(m => new { Name = m.Groups[1].Value, Relationship = m.Groups[2].Value })
  6.    .GroupBy(a => a.Relationship, a => a.Name);

F#
Code - F#: [Select]
  1. let query =
  2.    names
  3.    |> Seq.map(fun n -> pattern.Match(n))
  4.    |> Seq.choose(fun m -> if m.Success then Some (m.Groups.[1].Value, m.Groups.[2].Value) else None)
  5.    |> Seq.groupBy snd
  6.    |> Seq.map(fun (k, v) -> (k, Seq.map fst v))
Speaking English as a French Frog

kdub

  • SuperMod
  • Swamp Rat
  • Posts: 978
  • class keyThumper<T>:ILazy<T>
Re: (vent) Introduction to LINQ
« Reply #6 on: January 14, 2016, 03:07:02 am »
Hi,

Tell me if I'm off topic, but I really like these drunk guys who brought some functional programming features in C# with Linq.
< .. >

You're not off topic ... and I agree. I didn't want to scare John away, but he's a big boy, so he'll have to get over it :)
He's scared of clowns too, so I'm a little worried about him, really.
called Kerry in my other life

Sometimes the question is more important than the answer.

CADbloke

  • Bull Frog
  • Posts: 281
  • Crash Test Dummy
Re: (vent) Introduction to LINQ
« Reply #7 on: January 14, 2016, 08:40:21 am »
If you don't like Linq then you're gunna hate what I'm doing at the moment ... http://reactiveui.net/ & http://reactivex.io/  (https://github.com/Reactive-Extensions/Rx.NET)

Why: This: http://docs.reactiveui.net/en/compelling-example/index.html

...edit... the manual: http://www.introtorx.com/Content/v1.0.10621.0/01_WhyRx.html

The course is by Jon Skeet and Rob Conery. My next course was going to be the advanced courses from them.

FYI, John Skeet is the Tony Tanzillo of .NET (but much more polite), everything he does is advanced and brainsploding.
« Last Edit: January 19, 2016, 12:51:09 am by CADbloke »

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 8840
Re: (vent) Introduction to LINQ
« Reply #8 on: January 14, 2016, 10:57:55 am »
lol I know, I know guys. I'll get over my "weirdness" soon and be coding in no time.

I like that chaning method better, gile.

Not that my "weirdness" matters much but, overall I just feel..."dirty" (unclean) when I am playing with C# for some reason. I know it's type safe and everything but it's the little things that seem to be getting at me for some odd reason. Like VAR (because you used it in your example Gile). I'd much rather write out something like:

Code - C#: [Select]
  1. Foo<string> myFooInstance = new Foo<string>("blah");

instead of

Code - C#: [Select]
  1. var myFooInstance = new Foo<string>("blah");

I mean I understand what the compiler is doing with VAR and I know it's type safe but I just cringe when I see this kind of stuff for some reason but, I'll get over it.

As far as Swift goes; Microsoft is building a compiler to compile Swift code. ...Swift code (apple stuff now) is a bit different then the MS ecosystem stuff; it's compiled and built with the Clang compiler (the compiler that puts all others to shame). It was the compiler that, I'm sure you've seen me post about, that sorta "understands" your code (think "intelliSense" but contextually). The IR code Clang produces is very, very "tidy" -e.g. You write C++ code and you can take the IR code from Clang and generate JavaScript code from it (there are backend code generators now which is very, very cool stuff). Basically, Swift is planned to be supported by Microsoft because of the plan to support a vast range of other stuff (namely: android & IOS).

Now, I have to get back to fighting with VS Community and unit tests. Later and thanks guys.
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 8840
Re: (vent) Introduction to LINQ
« Reply #9 on: January 14, 2016, 12:19:05 pm »
<snip>
FYI, John Skeet is the Tony Tanzillo of .NET (but much more polite), everything he does is advanced and brainsploding.

Tony is the Tony of .NET. ...I think you are trying to say that Jon is a good programmer (I understand that; I googled each of the authors before taking the class) but that was a very odd comparison.

BTW, what is your obsession with Tony's "politeness"? Almost every example I've ever seen Tony give was complete and concise; he spent time making sure his points are completely clear. You should see some of the threads in the Unix newsgroups; they are chalked full of truly genius programmers (the ones responsible for things like "how your computer connects to the internet" or "making sure your online banking is safe") who do not beat around any bushes whatsoever.
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 8840
Re: (vent) Introduction to LINQ
« Reply #10 on: January 14, 2016, 12:32:06 pm »
Theo de Raadt (creator of OpenBSD) is one guy that I think you'd partially get a kick out of.
[ https://en.wikipedia.org/wiki/Theo_de_Raadt ]

Quote
The world doesn't live off jam and fancy perfumes - it lives off bread and meat and potatoes. Nothing changes. All the big fancy stuff is sloppy stuff that crashes. I don't need dancing baloney - I need stuff that works. That's not as pretty, and just as hard.

Quote
Low code quality keeps haunting our entire industry. That, and sloppy programmers who don't understand the frameworks they work within. They're like plumbers high on glue.

Quote
So the HP guy comes up to me (at the Melbourne conference) and he says, 'If you say nasty things like that to vendors you're not going to get anything'. I said 'no, in eight years of saying nothing, we've got nothing, and I'm going to start saying nasty things, in the hope that some of these vendors will start giving me money so I'll shut up'.
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

MickD

  • Gator
  • Posts: 2947
  • I don't need a job, I need Money!!
Re: (vent) Introduction to LINQ
« Reply #11 on: January 14, 2016, 02:55:14 pm »
...
Not that my "weirdness" matters much but, overall I just feel..."dirty" (unclean) when I am playing with C# for some reason. I know it's type safe and everything but it's the little things that seem to be getting at me for some odd reason. Like VAR (because you used it in your example Gile). I'd much rather write out something like:

Code - C#: [Select]
  1. Foo<string> myFooInstance = new Foo<string>("blah");

instead of

Code - C#: [Select]
  1. var myFooInstance = new Foo<string>("blah");

I mean I understand what the compiler is doing with VAR and I know it's type safe but I just cringe when I see this kind of stuff for some reason but, I'll get over it.
...

Type inference is a good thing, let the compiler work it out and save me some needless typing at the keyboard.
I cringe when I have to type -
Code - C#: [Select]
  1. Foo<string> myFooInstance = new Foo<string>("blah");

:)

Yes, C# is getting convoluted/polluted (however you want to look at it) and it's why I prefer F# these days. When languages like C++, C# and Java keep adding 'features' to the language you get the feeling they did something wrong or left something out. They try to be 'all things' for all situations and the language pays the price. I prefer C over C++ any day, yes it lacks things but you can just make them yourself with a simple language.

F# rules are pretty straight forward and it is a functional first, strongly typed language that allows you to be very concise and to the point. It takes a bit of getting used to but I really enjoy using F# (no linq required ;)).
"A language that doesn’t have everything is actually easier to program in than some that do."

        — Dennis M. Ritchie

CADbloke

  • Bull Frog
  • Posts: 281
  • Crash Test Dummy
Re: (vent) Introduction to LINQ
« Reply #12 on: January 14, 2016, 03:27:48 pm »
John, I know you are probablyalready aware of tmost of this, I posted this for the general community so don't think I have underestimated you, please ...
I know it's type safe and everything but it's the little things that seem to be getting at me for some odd reason. Like VAR (because you used it in your example Gile). I'd much rather write out something like:

Code - C#: [Select]
  1. Foo<string> myFooInstance = new Foo<string>("blah");

instead of

Code - C#: [Select]
  1. var myFooInstance = new Foo<string>("blah");

I mean I understand what the compiler is doing with VAR and I know it's type safe but I just cringe when I see this kind of stuff for some reason but, I'll get over it.
I don't use var a lot. This SO post sums up my approach to var, although I don't have a problem with your example. You get used to reading it fairly quickly. There's a longer debate about it but the brief version sums it up well. Microsoft use var a lot more than I do, I don't like it much because it's harder to read code in places like Github, here etc. where there is no Intellisense. It makes perfect sense for anonymous types though. If you don't like it, Resharper can automatically fix all the var in your code.

As far as Swift goes; Microsoft is building a compiler to compile Swift code. ...Swift code (apple stuff now) is a bit different then the MS ecosystem stuff; it's compiled and built with the Clang compiler (the compiler that puts all others to shame). It was the compiler that, I'm sure you've seen me post about, that sorta "understands" your code (think "intelliSense" but contextually). The IR code Clang produces is very, very "tidy" -e.g. You write C++ code and you can take the IR code from Clang and generate JavaScript code from it (there are backend code generators now which is very, very cool stuff). Basically, Swift is planned to be supported by Microsoft because of the plan to support a vast range of other stuff (namely: android & IOS).

Now, I have to get back to fighting with VS Community and unit tests. Later and thanks guys.
Swift compiler? Intersting. Seen https://xamarin.com/studio ? The new Microsoft .NET does support Android, iOS etc and runs on Mac & Linux via DNX. I don't know enough about compilers to say anything meaningful other than to mention Roslyn seems to b well received. Transpiling C++ to JavaScript is damn impressive.

As far as unit testing goes, Microsoft has gone for XUnit in all their big projects. The ASP.NET Engineering guidelines are worth a look at to see what they are doing, both as guidance and also to see what you can expect in most of the code you will see from. Obviously you can take it or leave it but I, personally, tend to take guidance from the frameworks I am writing against so I am familiar with their style.

<snip>
FYI, John Skeet is the Tony Tanzillo of .NET (but much more polite), everything he does is advanced and brainsploding.

Tony is the Tony of .NET. ...I think you are trying to say that Jon is a good programmer (I understand that; I googled each of the authors before taking the class) but that was a very odd comparison.

BTW, what is your obsession with Tony's "politeness"? Almost every example I've ever seen Tony give was complete and concise; he spent time making sure his points are completely clear. You should see some of the threads in the Unix newsgroups; they are chalked full of truly genius programmers (the ones responsible for things like "how your computer connects to the internet" or "making sure your online banking is safe") who do not beat around any bushes whatsoever.
I never had a problem with Tony but you're right (on both counts), it's not relevant. And, yes, Linus Torvalds' demeanour is infamous and I am sure it is not uncommon. In a cruel irony (for Linux fanbois) I have found the .NET communities to be overwhelmingly civil.
« Last Edit: January 14, 2016, 05:45:57 pm by CADbloke »

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 8840
Re: (vent) Introduction to LINQ
« Reply #13 on: January 14, 2016, 04:02:03 pm »
Mick,
You hit the nail right on the head Mick. I defiantly get the feeling they left a bunch out and should have just nailed down the specs before hand.

I will let the compiler do the work (just needed to complain a little I guess). And, for what it's worth, that isn't that much more typing. ;).

CADbloke,
The new .NET compiler supporting Android and iOS is still a .NET language; wrappers and all that messy stuff. Not the same thing at all (I'll leave that up to you to read up on). The GPLv3/Clang story is like a soap opera; you should read up on it. Tons of fun. What developers did with Clang/LLVM is nothing short of amazing! ...But, compilers themselves are very dry and boring subjects to be honest. Most people are happy just using them and not worrying about creating them. If you want to get into that aspect (compilers), let me know and I'll direct you where to start but, again, it's a very dry--but fascinating--subject.

I am happy unit testing in very crude ways so I am good with the built in solutions.

I keep hearing about this Reshaper. I looked it up and most of it is just gibberish to me (of course in all fairness, I don't care much about the features it may or may not have because I am used to doing things the long way anyways). But anyways, thanks for the tip about Reshaper (I will not use it though, sorry).
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

Jeff H

  • Needs a day job
  • Posts: 5853
Re: (vent) Introduction to LINQ
« Reply #14 on: January 14, 2016, 04:06:05 pm »
When creating a new object it is obvious what type var is but for functions that return types I like to use var because it helps to see if your naming your functions correctly.