select 名字,sum(数量)
from tb
group by 名字select 名字,sum(数量) as '数量'
from tb
group by 名字

解决方案 »

  1.   


    伤不起 弱弱问句linq语句是什么?
      

  2.   

    /// <summary>
    /// 求和
    /// </summary>
    public void Linq4()
    {
        int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };    double numSum = numbers.Sum();    Console.WriteLine("The sum of the numbers is {0}.", numSum);
    }---结果       The sum of the numbers is 45.
      

  3.   

    1.Sum1.1 需要分组                var query1 = (from PlantEnergyView in m_Context.PlantEnergyView
                                  where PlantEnergyView.UsageDate >= mintime
                                  && PlantEnergyView.UsageDate <= maxtime
                                  && PlantEnergyView.ProducedDate >= mintime
                                  && PlantEnergyView.ProducedDate <= maxtime
                                  orderby PlantEnergyView.MediumID ascending
                                  group PlantEnergyView by PlantEnergyView.MediumID into g
                                  select new
                                  {
                                      MediumID = g.Key,
                                      Usage = g.Sum(p => p.Usage),
                                      TotalMonth = g.Max(p => p.TotalMonth),
                                      TotalYear = g.Max(p => p.TotalYear)
                                  }
                             ).ToList();1.2不需要分组            var query2 = (from ActualProduced in m_Context.ActualProduced
                             where ActualProduced.Date >= mintime
                             && ActualProduced.Date <= maxtime
                             && ActualProduced.DepartmentID == 7
                             select ActualProduced.ProductQty).Sum();1.3多个分组依据var query2 = (from UsageView in m_Context.UsageView
                                  where UsageView.UsageDate >= mintime
                                       && UsageView.UsageDate <= maxtime
                                       && UsageView.MediumID != null
                                       && UsageView.CategoryID != null
                                       && UsageView.DepartmentID != 22
                                       && (UsageView.ShowRecoredReport == 1 || UsageView.ShowRecoredReport == ShowReport)
                                       && (UsageView.CategoryActive == 1 || UsageView.CategoryActive == CattegoryActive)
                                  group UsageView by new
                                  {
                                      UsageView.MetricPointID,
                                      UsageView.CategoryName,
                                      UsageView.CategorySort,
                                      UsageView.DepartmentID,
                                      UsageView.DepartmentsSort,
                                      UsageView.MediumID,
                                      UsageView.MediumSort
                                  } into g
                                  select new
                                  {
                                      g.Key,
                                      g,
                                      Usage = g.Sum(p => p.Usage),
                                      TotalMonth = g.Max(p => p.TotalMonth),
                                      TotalYear = g.Max(p => p.TotalYear)
                                  }).ToList();