c# – LINQ to SQL使用不同的行对多个列进行分组
发布时间:2020-12-16 01:37:21 所属栏目:百科 来源:网络整理
导读:我有以下表格结构.我想选择不同的CustomerId和CustomerName,TotalCost. 这是表结构和列数据类型. LogId (int)CustomerId (string)CustomerName (string)Cost (int) Logid / CustomerId / CustomerName / Cost 1 2031 John Steward 20 2 2035 Mary Joe 10 3 2
我有以下表格结构.我想选择不同的CustomerId和CustomerName,TotalCost.
这是表结构和列数据类型. LogId (int) CustomerId (string) CustomerName (string) Cost (int)
> 1 2031 John Steward 20
这是我到目前为止的尝试查询: 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)} 但它使错误消息Range变量名只能从没有参数的简单或限定名称推断.
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 错误:未声明名称’queryResult’. 解决方法
如果我正确理解您的要求,这样的事情应该在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) }; [编辑] 我想我最初想的为什么它不起作用是错误的.我们的语法错误了. Dim query = From log In db.LogRecords _ Group log By log.CustomerId,log.CustomerName _ Into Cost = Sum(log => log.Cost) _ Select CustomerId,Cost (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |