LINQ和按周分组

J

我有一个字符串字段,其中包含我转换为日期的日期。我需要按星期使用LINQ对其进行分组。我最终的目标是



BeginDateOfWeek(每周从星期一开始)

BeginDateOfWeek实际上将在报表中使用。年和周在那里提供一个函数(尚未编写,但是一旦我弄清楚就可以了),该函数将确定BeginDateOfWeek中的日期信息。

但是,当我用两个函数i将此日期字段分组为YEAR和WEEK时,我得到:

YEAR = 2020,WEEK =生成方法

我为每个设置了一个,并且确定每周都有足够的列为WEEK =生成的方法。

因此,我尝试将join子句中的函数重命名(只是对正在发生的事情进行吐口水,所以我尝试了一下),但出现错误:

“错误BC36621'等于'无法将'函数(i作为对象)作为整数'的值与'函数(i作为对象)作为整数的值'进行比较。”

因此,如果有人可以帮助我,我将非常感激。这是vb.net(C#解决方案很好),不是Linq To Entites,而是常规LINQ。我真是机智。

Dim query = From CCM In tbl1 Group CCM _
            By Year = CType(CCM.LCDATE, Date).Year,
            Week = (Function(i) System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(i.LCDATE, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday)) Into grp = Group
            Group Join CCT In (From CCTS In tbl2
                               Group CCTS _
                               By Year1 = CType(CCTS.CCDATE, Date).Year, Week1 = (Function(j) System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(j.CCDATE, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday))
                               Into grp1 = Group) 
            On Year Equals CCT.Year1 And Week Equals CCT.Week1
            Into Merged = Group From CCT In Merged.DefaultIfEmpty()
            Select New With {.YEAR = Year, .WEEK = Week, .MyTest = "")}
pwilcox

正如错误所言,

(Function(i) System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(i.LCDATE, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday))

与之相对应的是功能。

您实际上需要执行以下功能:

(Function(i) System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(i.LCDATE, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday))(CType(CCM.LCDATE, Date)) 

或者,最好在其他地方定义该函数,然后在查询中使用它:

Function getWeekOfYear (str As String) As Integer

    Return System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
        CType(str, Date), 
        System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday
    )

End Function


Dim query = 
    From CCM In tbl1 
    Group CCM By 
        Year = CType(CCM.LCDATE, Date).Year,
        Week = getWeekOfYear(CCM.LCDATE) Into grp = Group
        Group Join CCT In (
            From CCTS In tbl2
            Group CCTS By 
                Year1 = CType(CCTS.CCDATE, Date).Year, 
                Week1 = getWeekOfYear(CCTS.CCDATE)
                Into grp1 = Group
        ) 
        On Year Equals CCT.Year1 And Week Equals CCT.Week1
        Into Merged = Group 
    From CCT In Merged.DefaultIfEmpty()
    Select New With {.YEAR = Year, .WEEK = Week, .MyTest = ""}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档