This is a multi-part message in MIME format.
------=_NextPart_000_04B3_01C995BE.32A3F2E0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi,=20
If I have a ADO.NET dataTable contains information like
OrderID, State, SalesAmnt
=3D=3D=3D=3D=3D=3D =3D=3D=3D =3D=3D=3D=3D=3D=3D=3D
1 CA 100
2 TX 30
3 WA 50
4 TX 120
5 CA 20
What Linq syntax would be able to produce same results as
SELECT Sum(SalesAmt) As TotalSales, Count(OrderID) As NumberOfOrder
FROM Table
GROUP BY State
If not possible by Linq, Can it be done using SQL Compact?
Thanks very much!
John
I'm not exactly sure what you meant by a DataTable in ado.net... But, I'm
assuming you meant you have a populated DataTable object and want to query it.
Well, it is possilbe - using the System.Data.DataTableExtension methods:
Option Strict On
Option Explicit On
Imports System
Imports System.Data.DataTableExtensions
Module Module1
Sub Main()
Dim table As DataTable = GenerateTable()
Dim sales = From o In _
(From row In table.AsEnumerable() Select New With {.ID = row.Field(Of Integer)(0), .State = row.Field(Of String)(1), .Amount = row.Field(Of Decimal)(2)}) _
Group By o.State Into TotalSales = Sum(o.Amount), NumberOfOrders = Count()
For Each sale In sales
Console.WriteLine("State={0}, Sum={1}, Orders={2}", sale.State, sale.TotalSales, sale.NumberOfOrders)
Next
End Sub
Private Function GenerateTable() As DataTable
Dim dt As New DataTable("SalesInfo")
dt.Columns.Add(New DataColumn("OrderId", GetType(Integer)))
Dim stateColumn As New DataColumn("State", GetType(String))
stateColumn.MaxLength = 2
dt.Columns.Add(stateColumn)
dt.Columns.Add(New DataColumn("SalesAmnt", GetType(Decimal)))
dt.Rows.Add(1, "CA", 100)
dt.Rows.Add(2, "TX", 30)
dt.Rows.Add(3, "WA", 50)
dt.Rows.Add(4, "TX", 120)
dt.Rows.Add(5, "CA", 20)
dt.AcceptChanges()
Return dt
End Function
End Module
HTH