C# 使用LINQ对数据表进行分组
我有以下数据集和3个表C# 使用LINQ对数据表进行分组,c#,linq,C#,Linq,我有以下数据集和3个表 DataSet ds = new DataSet(); DataTable table1 = new DataTable("table1"); table1.Columns.Add("ParentId", typeof(int)); table1.Columns.Add("ParentName", typeof(string)); table1.Rows.Add(1, "Name1"); table1.Rows.Add(2, "Name2
DataSet ds = new DataSet();
DataTable table1 = new DataTable("table1");
table1.Columns.Add("ParentId", typeof(int));
table1.Columns.Add("ParentName", typeof(string));
table1.Rows.Add(1, "Name1");
table1.Rows.Add(2, "Name2");
DataTable table2 = new DataTable("table2");
table2.Columns.Add("ChildId", typeof(int));
table2.Columns.Add("ParentId", typeof(int));
table2.Columns.Add("ChildRowName", typeof(string));
table2.Columns.Add("ChildColumnName", typeof(string));
table2.Rows.Add(1, 1, "ChildRowName1", "Column1");
table2.Rows.Add(2, 1, "ChildRowName1", "Column2");
table2.Rows.Add(3, 2, "ChildRowName2", "Column3");
table2.Rows.Add(3, 2, "ChildRowName2", "Column4");
DataTable table3 = new DataTable("table4");
table3.Columns.Add("ChildListId", typeof(int));
table3.Columns.Add("ChildId", typeof(int));
table3.Columns.Add("Attribute1", typeof(string));
table3.Columns.Add("Attribute2", typeof(string));
table3.Rows.Add(1, 1,"=", "Equals");
table3.Rows.Add(2, 1, "=", "Equals");
table3.Rows.Add(2, 2, "Cont", "Contains");
table3.Rows.Add(2, 3, "<", "LessThan");
我有下面的类来填充Datatable中的列表
public class Parent
{
public string Name { get; set; }
public IEnumerable<Child> childs { get; set; }
}
public class Child
{
public string Name { get; set; }
public IEnumerable<ChildList> items { get; set; }
}
public class ChildList
{
public string Name { get; set; }
public IEnumerable<ChildListAttribute> attributes { get; set; }
}
public class ChildListAttribute
{
public string ColumnHeader { get; set; }
public string Attribute1 { get; set; }
public string Attribute2 { get; set; }
}
我使用的是低于LINQ的查询,但Child始终为零
from p in ds.Tables[0].AsEnumerable()
select new Parent
{
Name = p["ParentName"].ToString(),
childs = from c in ds.Tables[1].AsEnumerable()
where p["ParentId"] == c["ParentId"]
group c by c["ChildRowName"] into groupitems
select new Child
{
Name = groupitems.First().Field<string>("ChildRowName").ToString(),
items = from item in groupitems
select new ChildList
{
Name = item["ChildColumnName"].ToString(),
attributes = from op in ds.Tables[2].AsEnumerable()
where item["ChildId"] == op["ChildId"]
select new ChildListAttribute
{
Attribute1 = op["Attribute1"].ToString(),
Attribute2 = op["Attribute2"].ToString(),
}
}
}
}).ToList();
您能告诉我查询有什么问题吗?首先,您需要在运行查询之前将表添加到数据集中,您可能已经在执行查询,但只想确保:
ds.Tables.Add(table1);
ds.Tables.Add(table2);
ds.Tables.Add(table3);
由于where子句,未填充子项:
where p["ParentId"] == c["ParentId"]
应该是:
where p["ParentId"].ToString() == c["ParentId"].ToString()
或
这是因为row[column]返回的是一个对象,而不是存储在列中的值。所以当你比较它们的时候,你是在比较物体本身是否相等,而事实并非如此。您希望测试这些对象的值是否相等
您需要做同样的事情,通过连接值而不是对象本身来获得属性
where item["ChildId"] == op["ChildId"]
应该是
where item["ChildId"].ToString() == op["ChildId"].ToString()
请告诉我们您是否有此功能。Hi McCee,我如何在上述查询中的分组记录中包含OrderBy?您可以在from和select之间添加OrderBy p.ParentName或任何要排序的字段。命令的顺序是:from-then-group,然后where-then-orderby,然后select。
where item["ChildId"].ToString() == op["ChildId"].ToString()