Reputation: 1950
I have the following table structure. I want to select distinct CustomerId
and CustomerName
, TotalCost
.
Here's the table structure and column data type.
LogId (int)
CustomerId (string)
CustomerName (string)
Cost (int)
Logid / CustomerId / CustomerName / Cost
Update 1
Here's my attempted query so far:
Dim db As New DemoDataContext()
Dim query = From log In db.LogRecords _
Where log.Cost> 10 _
Group log By New With {log.CustomerId, log.CustomerName} Into g() _
Select New With {g.CustomerId, g.CustomerName, .Cost = g.Sum(Function(log) log.Cost)}
But it makes error message Range variable name can be inferred only from a simple or qualified name with no arguments.
Update 2
Dim queryResult = (From log In db.LogRecords _
Group log By log.CustomerId, log.CustomerName Into Cost = Sum(log => log.Cost ) _
Select New With { CustomerId, CustomerName, TotalCost })
For Each q In queryResult
Next
Error : Name 'queryResult' is not declared.
Upvotes: 3
Views: 6693
Reputation: 134841
If I understand your requirements correctly, something like this should work in C#:
var query = from row in dataTable
group row by new { row.CustomerId, row.CustomerName } into g
select new
{
g.Key.CustomerId,
g.Key.CustomerName,
Cost = g.Sum(row => row.Cost)
};
[edit]
I guess my initial thought on why it didn't work was wrong. We just had wrong syntax.
Dim query = From log In db.LogRecords _
Group log By log.CustomerId, log.CustomerName _
Into Cost = Sum(log => log.Cost) _
Select CustomerId, CustomerName, Cost
Upvotes: 4
Reputation: 2421
Just an observation. According to what Jeff M wrote and update in the question.
Dim queryResult = (From log In db.LogRecords_
Where log.Cost > 10 _
Group log By log.CustomerId, log.CustomerName Into Cost = Sum(log.Cost) _
Select New With {CustomerId, CustomerName, Cost})
Upvotes: 3