Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何对从SQL server接收的数据进行分组_C#_Linq - Fatal编程技术网

C# 如何对从SQL server接收的数据进行分组

C# 如何对从SQL server接收的数据进行分组,c#,linq,C#,Linq,这是我从SQLServer接收到的数据 +--------+--------------+ | Role | DocumentType | +--------+--------------+ | Admin | Z24 | | Admin | Z25 | | User | Z24 | | User | Z25 | | DocUser| Z26 | | Admin | Z26

这是我从SQLServer接收到的数据

+--------+--------------+
| Role   | DocumentType |
+--------+--------------+
| Admin  | Z24          |
| Admin  | Z25          |
| User   | Z24          |
| User   | Z25          |
| DocUser| Z26          |
| Admin  | Z26          |
| User   | Z26          |
| Admin  | Z27          |
+--------+--------------+
我想用C语言将上述数据存储在如下列表中#

有人能帮我吗

这里有一个类似的例子

class Document
{
    public string DocType { get; set; }
    public string DocName { get; set; }
    public int Value { get; set; }
}
class Program
{
   static void Main(string[] args)
   {
      var docs = new List<Document>();
      docs.Add(new Document { DocType = "Main", DocName = "A", Value = 1 });
      docs.Add(new Document { DocType = "Main", DocName = "B", Value = 2 });
      docs.Add(new Document { DocType = "Main", DocName = "C", Value = 3 });
      docs.Add(new Document { DocType = "Main", DocName = "D", Value = 4 });
      docs.Add(new Document { DocType = "Main1", DocName = "D", Value = 4 });
      docs.Add(new Document { DocType = "Main1", DocName = "D", Value = 4 });
      var groupedData = docs.GroupBy(d => d.DocType).Select(group => group.ToList()).ToList();
   }
}
类文档
{
公共字符串DocType{get;set;}
公共字符串DocName{get;set;}
公共int值{get;set;}
}
班级计划
{
静态void Main(字符串[]参数)
{
var docs=新列表();
添加(新文档{DocType=“Main”,DocName=“A”,Value=1});
添加(新文档{DocType=“Main”,DocName=“B”,Value=2});
添加(新文档{DocType=“Main”,DocName=“C”,Value=3});
添加(新文档{DocType=“Main”,DocName=“D”,Value=4});
添加(新文档{DocType=“Main1”,DocName=“D”,Value=4});
添加(新文档{DocType=“Main1”,DocName=“D”,Value=4});
var groupedData=docs.GroupBy(d=>d.DocType);
}
}

如果只想检索部分列,请使用以下重载:

var result = data.GroupBy(key => key.Role, element => element.DocumentType);
否则,也可以使用此投影:

var result = data.GroupBy(key => key.Role)
                 .Select(g => new {
                     Role = g.Key,
                     Documents = g.Select(i => i.DocumentType)
                 });

你试过什么吗?看看linq的
GroupBy
不是免费的代码编写服务。您应该尝试自己编写代码。之后,如果你有问题,你可以张贴你已经尝试了一个明确的解释什么是不工作,并提供一个解决方案。我建议你读一个好的问题和答案。还有,一定要带上。我有……但没有拿到。然后请发布您尝试过的内容,解释什么不起作用,以及您试图解决的问题:)@Ravi:包括该代码,让我们看看fix@Ravi-对于下一个问题,请记住张贴您针对特定问题所做的尝试。那样的话,就不会有那么多的沮丧了。这是我第一次问问题。下次会记住这一点。:)@拉维-我知道:)这就是为什么我在发布答案之前花了很长时间。你可能想看看
var result = data.GroupBy(key => key.Role)
                 .Select(g => new {
                     Role = g.Key,
                     Documents = g.Select(i => i.DocumentType)
                 });