0

我有一个 DataTable 我想将其转换为列表

 var rows = cableDataTable.AsEnumerable()                            
 .Select((x) =>
  new
  {
     TagGroup = x.Field<string>("TagNo"),
     RowNo = (i == 0) ? (j++).ToString() : "",
     TagNo = x.Field<string>("TagNo"),
     FromBayPanel = x.Field<string>("FromBayPanel"),                                     
     Size = x.Field<string>("CoreSize"),
     Type = x.Field<string>("Type"),
     FromBay = x.Field<string>("FromBay"),
     FromPanel = x.Field<string>("FromPanel"),
     CoreNo = x.Field<string>("CoreNo") == DBNull.Value.ToString() ? "" : x.Field<string>("CoreNo"),
     RealCoreNo = x.Field<int>("RealCoreNo"),                                 
     FromDevice = x.Field<string>("FromDevice") == DBNull.Value.ToString() ? "" : x.Field<string>("FromDevice"),                                 
     FromTerminal = x.Field<string>("FromTerminal") == DBNull.Value.ToString() ? "" : x.Field<string>("FromTerminal"),
     FromRef = x.Field<string>("FromRef") == DBNull.Value.ToString() ? "" : x.Field<string>("FromRef"),
     ToBay = x.Field<string>("ToBay"),
     ToPanel = x.Field<string>("ToPanel"),
     ToDevice = x.Field<string>("ToDevice") == DBNull.Value.ToString() ? "" : x.Field<string>("ToDevice"),                                
     ToTerminal = x.Field<string>("ToTerminal") == DBNull.Value.ToString() ? "" : x.Field<string>("ToTerminal"),
     ToRef = x.Field<string>("ToRef") == DBNull.Value.ToString() ? "" : x.Field<string>("ToRef"),
     Remark = x.Field<string>("Remark") == DBNull.Value.ToString() ? "" : x.Field<string>("Remark"),
                             }).ToList();

我想必须对多个列上的 DataTable 进行分组。但是当我这样做时

1)我该怎么做?

2) 还有一个问题是在 SQL Server 端(通过 isNull 命令)还是客户端转换 DBNull 值更好?

4

2 回答 2

1

在 GroupBy 之后,您需要使用 sum 之类的聚合函数,就像在 sql 中一样。

           var resultSet = dataTable.GroupBy(e => new { e.Column1, e.Column2 }).Select(a => new { key1 = a.Key.Column1, Key2 = a.Key.Column2, TotalSize = a.Sum(b => b.Size), ConCatenatedTagGroups  = a.Select(c => c.TagGroup ).Aggregate((d, e) => d + e)});
于 2013-11-10T14:43:48.413 回答
1
var resultSet= dataTable.GroupBy(e=> new { e.Column1, e.Column2 });
于 2013-11-10T09:54:56.363 回答