小编use*_*595的帖子

如何在vb代码中的DataTable上正确执行linq中的"分组依据"?

为什么以下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)

linq vb.net

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

当种类为Utc时,从datetime UTC转换为datetimeoffset时出错

当类型为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)

c# timezone datetime datetimeoffset

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

标签 统计

c# ×1

datetime ×1

datetimeoffset ×1

linq ×1

timezone ×1

vb.net ×1