在 C# 中按总和 Linq to SQL 分组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16672632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 15:20:52  来源:igfitidea点击:

Group By Sum Linq to SQL in C#

sqllinqgroup-bysum

提问by vitalsigns.sa

Really stuck with Linq to SQL grouping and summing, have searched everywhere but I don't understand enough to apply other solutions to my own.

真的坚持使用 Linq to SQL 分组和求和,到处搜索,但我不明白将其他解决方案应用于我自己的解决方案。

I have a view in my database called view_ProjectTimeSummary, this has the following fields:

我的数据库中有一个名为 view_ProjectTimeSummary 的视图,它具有以下字段:

string_UserDescription
string_ProjectDescription
datetime_Date
double_Hours

I have a method which accepts a to and from date parameter and first creates this List<>:

我有一个方法,它接受一个 to 和 from 日期参数并首先创建这个 List<>:

List<view_UserTimeSummary> view_UserTimeSummaryToReturn = 
             (from linqtable_UserTimeSummaryView
              in datacontext_UserTimeSummary.GetTable<view_UserTimeSummary>()
              where linqtable_UserTimeSummaryView.datetime_Week <= datetime_To
              && linqtable_UserTimeSummaryView.datetime_Week >= datetime_From
              select linqtable_UserTimeSummaryView).ToList<view_UserTimeSummary>();

Before returning the List (to be used as a datasource for a datagridview) I filter the string_UserDescription field using a parameter of the same name:

在返回列表(用作 datagridview 的数据源)之前,我使用同名参数过滤 string_UserDescription 字段:

if (string_UserDescription != "")
        {
            view_UserTimeSummaryToReturn = 
                        (from c in view_UserTimeSummaryToReturn
                         where c.string_UserDescription == string_UserDescription
                         select c).ToList<view_UserTimeSummary>();
        }

return view_UserTimeSummaryToReturn;

How do I manipulate the resulting List<> to show the sumof the field double_Hours for that user and project between the to and from date parameters (and not separate entries for each date)?

我如何操作生成的 List<> 以显示该用户和项目之间的字段 double_Hours的总和(而不是每个日期的单独条目)?

e.g. a List<> with the following fields:

例如具有以下字段的 List<>:

string_UserDescription
string_ProjectDescription
double_SumOfHoursBetweenToAndFromDate

Am I right that this would mean I would have to return a different type of List<> (since it has less fields than the view_UserTimeSummary)?

我是否正确,这意味着我必须返回不同类型的 List<> (因为它的字段少于 view_UserTimeSummary)?

I have read that to get the sum it's something like 'group / by / into b' but don't understand how this syntax works from looking at other solutions... Can someone please help me?

我已经读过它来获得总和,它类似于“group/by/intob”,但从查看其他解决方案时不明白这种语法是如何工作的......有人可以帮助我吗?

Thanks Steve

谢谢史蒂夫

回答by Amy B

Start out by defining a class to hold the result:

首先定义一个类来保存结果:

public class GroupedRow
{
  public string UserDescription {get;set;}
  public string ProjectDescription {get;set;}
  public double SumOfHoursBetweenToAndFromDate {get;set;}
}

Since you've already applied filtering, the only thing left to do is group.

由于您已经应用了过滤,剩下要做的就是分组。

List<GroupedRow> result =
(
  from row in source
  group row by new { row.UserDescription, row.ProjectDescription } into g
  select new GroupedRow()
  {
    UserDescription = g.Key.UserDescription,
    ProjectDescription = g.Key.ProjectDescription,
    SumOfHoursBetweenToAndFromDate = g.Sum(x => x.Hours)
  }
).ToList();

(or the other syntax)

(或其他语法)

List<GroupedRow> result = source
  .GroupBy(row => new {row.UserDescription, row.ProjectDescription })
  .Select(g => new GroupedRow()
  {
    UserDescription = g.Key.UserDescription,
    ProjectDescription = g.Key.ProjectDescription,
    SumOfHoursBetweenToAndFromDate = g.Sum(x => x.Hours)
  })
  .ToList();