如何计算linq到sql查询中列值的总和?

beh*_*aqi 7 c# linq linq-to-sql

我写这个查询:

var query2 = (from p in behzad.Compare_closed_numbers_in_CRM_and_Billing_system_detail_counters
               where p.fileid == point.id
               select new
               {
                   p.count
               }).ToArray();
Run Code Online (Sandbox Code Playgroud)

在count列中有任何值,我想要计算所有的计数值.例如: 在此输入图像描述

我该如何植入?谢谢.

Sal*_*ari 5

如果count字段为int,请尝试:

int sum = behzad.Compare_closed_numbers_in_CRM_and_Billing_system_detail_counters
     .Where(t=>t.fileid == point.id)
     .Select(t => t.Count ?? 0).Sum();
Run Code Online (Sandbox Code Playgroud)

如果count字段是nvarchar(max)试试这个:

int sum = behzad.Compare_closed_numbers_in_CRM_and_Billing_system_detail_counters
         .Where(t=>t.fileid == point.id)
         .Select(t => Convert.ToInt32(t.Count)).Sum();
Run Code Online (Sandbox Code Playgroud)