'______________________________________________________ ' LINQ 2: Using Calculations ' Ron Kessler ' 2/9/09 ' _______________________________________________________ ' ' Make sure to look at the tables I use in my Object Designer and add them. ' This form shows how to grab data, make sure it is not null and if so put in a ' 0 in that column. Also shows how to make a calculation in a column. ' Introduces Lambda expressions & Nullable types ' Step 2: Look at the paging.aspx form. Make it the Startup form remember! Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim dc As New AdventureWorksDataContext '---in C# use Select New With (we are creating a new object) Dim products = From p In dc.Products _ Where p.ProductCategory.Name = "Mountain Bikes" _ Select New With {.Product_ID = p.ProductID, _ .Product_Name = p.Name, _ .Order_Count = p.SalesOrderDetails.Count, _ .Total_Sales = _ String.Format("{0:c}", If(p.SalesOrderDetails.Sum(Function(s) CType(s.OrderQty, Double?) * CType(s.UnitPrice, Double?)), 0))} '---the Function keyword is like the "=>" in C#. '---the If method above is new. If the expression to the right of If = null, then we tell it to use 0 instead. GridView1.DataSource = products GridView1.DataBind() End Sub End Class