gpt4 book ai didi

c# - LINQ to SQL - 按天/周/月分组

转载 作者:太空狗 更新时间:2023-10-30 01:10:28 26 4
gpt4 key购买 nike

我已经为这个问题摸不着头脑了一段时间。假设我做了一个扩展方法,要按日期对项目列表进行分组,我想更改可能的分组,以便结果可以按天、周或月进行分组。

我想到了以下内容,但我不断收到以下错误:

Method 'System.Object DynamicInvoke(System.Object[])' has no supported translation to SQL

方法:

public static IQueryable<IGrouping<MonthDateGroup, T>> GroupByDate<T>(
this IQueryable<T> items,
DateGroupFrequency grouping,
Expression<Func<T, DateTime?>> func)
{
var selector = func.Compile();
IQueryable<IGrouping<MonthDateGroup, T>> grouped = null;

if (grouping == DateGroupFrequency.Daily)
{
grouped = from a in items
let date = selector(a).Value
group a by new MonthDateGroup
{
Day = date.Day, Month = date.Month, Year = date.Year
}
into g
select g;
}
//Rest of groupings...

我假设在查询中使用 Func 是导致错误的原因,是否可以生成这样的代码?

附言我仍在努力思考 Func 和表达式 :)

最佳答案

组语法不应该是:

group a by new MonthDateGroup
{
a.Day = date.Day, a.Month = date.Month, a.Year = date.Year
}

关于c# - LINQ to SQL - 按天/周/月分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4679466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com