小编Leo*_*ton的帖子

使用Linq到GroupBy和Sum数据表

嗨,我有一个像这样的数据表:

Id             Amount 1        Amount 2        Amount 3  
1              2               2               2  
12             4               6               4  
12             6               6               5  
22             7               2               1  
22             7               2               2

我需要得到这样的数据表:

Id             Amount 1        Amount 2        Amount 3  
1              2               2               2  
12             10              12              9    
22             14              4               3

我最初尝试在匿名方法中执行此操作,但我需要将其返回到另一个无法使用匿名方法完成的类.我的第二次尝试是这样做,以便它可以返回:

DataTable ddt = dt.AsEnumerable()
        .Sum(g => g.Field<int>("Amount 1"))
        .GroupBy(g => new { Col1 = g["ID"] })
        .Select(g => g.OrderBy(r => r["ID"]).First())
        .CopyToDataTable();
Run Code Online (Sandbox Code Playgroud)

这段代码肯定不会编译但是如果可能的话,任何帮助/建议都会非常感激.我对linq很新.

c# linq datatable group-by sum

6
推荐指数
1
解决办法
2万
查看次数

将字符串转换为三个字母缩写

我最近获得了一个新的项目,将任何给定的字符串转换为1-3个字母的缩写.类似于我必须产生的东西的一个例子是下面给出的字符串可以是任何东西:

switch (string.Name)
        {
            case "Emotional, Social & Personal": return "ESP";
            case "Speech & Language": return "SL";
            case "Physical Development": return "PD";
            case "Understanding the World": return "UW";
            case "English": return "E";
            case "Expressive Art & Design": return "EAD";
            case "Science": return "S";
            case "Understanding The World And It's People"; return "UTW";

}
Run Code Online (Sandbox Code Playgroud)

我想我可以使用string.Split并计算数组中的单词数.然后添加处理特定长度字符串的条件,因为这些句子通常不会长于4个字,但我将遇到的问题是.

  1. 如果字符串比我预期的长,则不会被处理
  2. 必须从缩写中排除符号

关于我可以应用的逻辑的任何建议将非常感激.谢谢

c# arrays string logic split

0
推荐指数
1
解决办法
1180
查看次数

标签 统计

c# ×2

arrays ×1

datatable ×1

group-by ×1

linq ×1

logic ×1

split ×1

string ×1

sum ×1