为什么以下group by子句不起作用?最初的问题是我如何在带有DataTable的vb代码(dot.net v4.0)中的LINQ中执行组并对组进行求和?这是样本,但它没有产生所需的输出.它返回2行而不是一行.
Dim table As New DataTable()
table.Columns.Add("GroupName", GetType(String))
table.Columns.Add("ProductName", GetType(String))
table.Columns.Add("QTY", GetType(Integer))
table.Rows.Add("a", "b", 1)
table.Rows.Add("a", "b", 1)
Dim testQuery =
(From e In table
Group e By Key = New With {
.GroupName = e("GroupName"),
.ProductName = e("ProductName")
} Into Group
Select New With {
.ProductName = Key.ProductName,
.QTY = Group.Sum(Function(x) Convert.ToInt32(x("QTY"))),
.GroupName = Key.GroupName
})
Run Code Online (Sandbox Code Playgroud) 当类型为Utc时,将UTC中的DateTime转换为DateTimeOffset时出错。origDateTime来自Web服务,因此我无法控制内容或格式。在大多数情况下,它带有Kind = Unspecified(即使在Utc中,即使时间很艰难)也可以正常工作,但是在极少数情况下,Kind = Utc然后转换为DateTimeOffset会引发异常:“ Utc DateTime实例的UTC偏移量必须为0。\ r \ n参数名称:offset“我应如何解决?
try {
//cause error !!!!
DateTime databaseUtcTime = DateTime.Parse("4/2/2016 6:25:20 PM");
var localTimeTemp = databaseUtcTime.ToLocalTime();
DateTime origDateTime = localTimeTemp.ToUniversalTime();
//this is working
//DateTime origDateTime = DateTime.Parse("4/2/2016 6:25:20 PM");
string timeZoneName = "Pacific Standard Time";
TimeZoneInfo localTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName);
DateTimeOffset localTime = new DateTimeOffset(origDateTime, localTimeZone.GetUtcOffset(origDateTime));
return localTime;
}
catch (Exception ex) {
string msg = ex.Message;
return null;
}
Run Code Online (Sandbox Code Playgroud)