2016-03-30 117 views
2

我正在尋找在以下LINQ查詢和擴展方法幫助GetYearWeekFormat擴展: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. 

有人可以幫忙嗎?

+0

您是否將此與Entity Framework一起使用? – MattC

+0

這也不起作用,並非所有的查詢語句都可以轉換爲sql查詢語句,特別是那些包含像GetYearWeekFormat類似的自定義邏輯的查詢語句。 –

+0

不是這是一箇舊系統 - 它使用Linq 2 Sql - 一個dbml文件 – Wattcey

回答

1

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 
     });