Another Way to LINQ

Published 13 September 07 01:12 PM

I was reading another installment of ScottGu's articles on his blog about using LINQ to SQL in ASP.NET with the LinqDatasource. Scott does an amazing job of explaining things very clearly so if you haven't checked his posts out already then you definitely should. It's also very nice to see him show both VB and C# code. One example that I want to point out is where he uses a Lambda Expression to get the totals for each product:

  Dim products = From p In db.Products _

                 Where p.Category.CategoryName.StartsWith("C") _

                 Select p.ProductID, _

                   p.ProductName, _

                   p.UnitPrice, _

                   NumOrders = p.Order_Details.Count, _

                   Revenue = p.Order_Details.Sum(Function(details) _

                             details.UnitPrice * details.Quantity)

 

The lambda here is denoted by the "Function" keyword and it is passed to the "Sum" extension method in order to calculate the order totals for each product. In VB 9 we can also use "Aggregate" instead in this case. Additionally, we can use "Like" in the Where clause producing the following equivalent query:

 

Dim products = From p In db.Products _

               Where p.Category.CategoryName Like "C*" _

               Select p.ProductID, _

                p.ProductName, _

                p.UnitPrice, _

                NumOrders = p.Order_Details.Count, _

                Revenue = _

                   Aggregate detail In p.Order_Details _

                   Into Sum(detail.UnitPrice * detail.Quantity)

VB 9 has additional LINQ expressions that you can use instead of manually writing lambda expressions for things like Sum, Min, Max, Average, etc. I prefer the easier-to-understand VB expression syntax so I tend to use these instead. For more information on writing group and aggregate queries in VB 9 watch this video.

 

Enjoy!

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# MSDN Blog Postings » Another Way to LINQ said on September 13, 2007 6:59 PM:

PingBack from http://msdnrss.thecoderblogs.com/2007/09/13/another-way-to-linq/

# buaziz said on September 18, 2007 2:14 PM:

thanks , i was looking for LINQ VB samples

:)

# Beth Massi said on September 18, 2007 4:01 PM:

Hi buaziz,

You can also check out the VB LINQ How-Do-I videos here: http://msdn2.microsoft.com/en-us/vbasic/bb466226.aspx?wt.slv=topsectionsee#linq

Cheers,

-B

# Didier LEVY said on July 15, 2009 2:59 PM:

Hi, could you please help me? I need to retrieve and display the following (from Northwind as an example):

CustomerID   Number of Orders  Total Ordered

ALFKI               12                    $1234.60

BONAPP             6                        $41.50

Which need a join between 3 tables.

And I would like it in LINQ, of course!

The first 2 columns I've no problem, here it is:

Dim orderQuery2 = From c In db.Customers _

               Where c.Country = selectedCountry _

               Select c.CustomerID, c.CompanyName, _

               NumOrders = c.Orders.Count

But I'm just pulling my hairs out of my head when it comes to adding the 3rd colum.

Any clue?

If not, then I've to do the job with two separate LINQ queries, and then I'll be dishonoured!

TIA for your help.

Didier

# Beth Massi said on July 16, 2009 2:50 PM:

Hi Didier,

Just use the aggregate clause like I show above. Is that not working for you?

-B

# bashir said on August 31, 2009 4:46 AM:

oooooooooh massi she is my lovely teacher

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

About Beth Massi

Beth is a Program Manager on the Visual Studio Community Team at Microsoft and is responsible for producing and managing content for business application developers, driving community features and team participation onto MSDN Developer Centers (http://msdn.com), and helping make Visual Studio one of the best developer tools in the world. She also produces regular content on her blog (http://blogs.msdn.com/bethmassi), Channel 9, and a variety of other developer sites and magazines. As a community champion and a long-time member of the Microsoft developer community she also helps with the San Francisco East Bay .NET user group and is a frequent speaker at various software development events. Before Microsoft, she was a Senior Architect at a health care software product company and a Microsoft Solutions Architect MVP. Over the last decade she has worked on distributed applications and frameworks, web and Windows-based applications using Microsoft development tools in a variety of businesses. She loves teaching, hiking, mountain biking, and driving really fast.

This Blog

Syndication

Page view tracker