gpt4 book ai didi

c# - Linq 扩展方法 - GetYearWeekFormat 扩展

转载 作者:太空宇宙 更新时间:2023-11-03 12:48:58 25 4
gpt4 key购买 nike

我正在寻求有关以下 linq 查询和扩展方法的帮助:

DateTime? ddStartOfTime = Convert.ToDateTime("2016/04/10 11:15:00");
DateTime? ddEndOfTime = Convert.ToDateTime("2017/06/25 13:15:00");

List<int> doWeeksNeeded = new List<int>();

doWeeksNeeded.Add(201614);
doWeeksNeeded.Add(201616);

var dq1 = (from c in tblschedulefulldates
where doWeeksNeeded.Contains(c.ddJobStart.GetYearWeekFormat())
orderby c.diEmployeeID
select new
{
diEmployeeID = c.diEmployeeID == null ? 0 : (int)c.diEmployeeID,
dnDayOfWeek = c.ddJobStart.DayOfWeek == null ? 0 : (int)c.ddJobStart.DayOfWeek
});

以下是扩展方法:

public static int GetYearWeekFormat(this DateTime pddate)
{
int weekNo = GetIso8601WeekOfYear(pddate);
int year = pddate.Year;
if (weekNo == 1 && pddate.Month > 1) year = year + 1;
return Convert.ToInt32(year.ToString() + weekNo.ToString().Trim());
}

public static int GetIso8601WeekOfYear(this DateTime pddate)
{
System.Globalization.Calendar cal = System.Globalization.CultureInfo.InvariantCulture.Calendar;
// Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll
// be the same week# as whatever Thursday, Friday or Saturday are,
// and we always get those right
System.DayOfWeek day = cal.GetDayOfWeek(pddate);
if (day >= System.DayOfWeek.Monday && day <= System.DayOfWeek.Wednesday)
{
pddate = pddate.AddDays(3);
}

// Return the week of our adjusted day
return cal.GetWeekOfYear(pddate, System.Globalization.CalendarWeekRule.FirstFourDayWeek, System.DayOfWeek.Monday);
}

但是当我运行它时我得到:

Method 'Int32 GetYearWeekFormat(System.DateTime)' has no supported translation to SQL

所以我决定创建另一个名为 In 的扩展:

public static bool In<T>(this DateTime value, IEnumerable<Int32> values)
{
if (values == null)
throw new ArgumentNullException("values");
int lnWeek = GetYearWeekFormat(value);
return values.Contains(lnWeek);
}

然后这样调用它:

var dq1 = (from c in tblschedulefulldates
where c.ddJobStart.In(doWeeksNeeded)
orderby c.diEmployeeID
select new
{
diEmployeeID = c.diEmployeeID == null ? 0 : (int)c.diEmployeeID,
dnDayOfWeek = c.ddJobStart.DayOfWeek == null ? 0 : (int)c.ddJobStart.DayOfWeek
});

除非我也无法让它工作。它给出了以下错误:

The type arguments for method 'DateTimeExtensions.In<T>(DateTime, IEnumerable<int>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

有人可以帮忙吗?

最佳答案

LINQ 2 SQL 不知道如何将 GetYearWeekFormat 转换为 SQL。

您需要先查询数据库,然后再对本地列表进行操作。

var localList = (from c in tblschedulefulldates
select new
{
ddJobStart= c.ddJobStart,
diEmployeeID= c.diEmployeeID,
}).ToList();

var dq1 = (from c in localList
where doWeeksNeeded.Contains(c.ddJobStart.GetYearWeekFormat())
orderby c.diEmployeeID
select new
{
diEmployeeID = c.diEmployeeID == null ? 0 : (int)c.diEmployeeID,
dnDayOfWeek = c.ddJobStart.DayOfWeek == null ? 0 : (int)c.ddJobStart.DayOfWeek
});

关于c# - Linq 扩展方法 - GetYearWeekFormat 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36306270/

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