Grouping is another powerful tool in the LINQ library. This gives you the ability to group data in specific ways. Take a look at the example by clicking more below.
One really cool thing with C# .NET LINQ is the ability to take a query and output the data to an anonymous types. This work with either type of query be it comprehension or method syntax. The one drawback is that you can’t access this directly because it inherits directly from object. You would need to cast it as new defined type (class) to do this. You will see me doing this different ways thru the upcoming examples. How you choose to do it is based on your own personal preferences.
Ahh type inferece. If you do any searching around stacktrace, code project, slashdot, etc.., you will find plenty of arguments for using strong typed variables or using the var keyword when declaring local variables. I’ve recently been using var keyword more in my code after reading C# In-Depth by Jon Skeet. The C# compiler does a good job if making sure things are safe at compile time. For example var stringList = new List<string>() is the same as List<string> stringList = new List<string>(). So it all comes down to personal preference when using C# .NET.
For this example I’m going to be showing some different ways to LINQ query syntax on a database and some in-memory objects. I’ll be showing the two main different types of queries. Extension and method syntax.
In the coming posts you will see me add a little more complexity. Why for such a simple console program? Well it can just show some basic concepts that can be used in larger, more user friendly programs.
This example doing LINQ To SQL from a simple database where I’m using a couple examples of using LINQ to query a SQL database for information.