title: Linq Extension Method - GetYearWeekFormat Extension
body: <p>I'm looking for help on the following linq query and extension method:</p>
  
  <pre><code>DateTime? ddStartOfTime = Convert.ToDateTime("2016/04/10 11:15:00");
  DateTime? ddEndOfTime = Convert.ToDateTime("2017/06/25 13:15:00");
  
  List&lt;int&gt; doWeeksNeeded = new List&lt;int&gt;();
  
  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
         });
  </code></pre>
  
  <p>The following are the Extension Methods:</p>
  
  <pre><code>public static int GetYearWeekFormat(this DateTime pddate)
  {
      int weekNo = GetIso8601WeekOfYear(pddate);
      int year = pddate.Year;
      if (weekNo == 1 &amp;&amp; pddate.Month &gt; 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 &gt;= System.DayOfWeek.Monday &amp;&amp; day &lt;= 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);
  }
  </code></pre>
  
  <p>However when I run this I get:</p>
  
  <pre><code>Method 'Int32 GetYearWeekFormat(System.DateTime)' has no supported translation to SQL
  </code></pre>
  
  <p>So I decided to create another extension called In:</p>
  
  <pre><code>public static bool In&lt;T&gt;(this DateTime value, IEnumerable&lt;Int32&gt; values)
  {
      if (values == null)
          throw new ArgumentNullException("values");
      int lnWeek = GetYearWeekFormat(value);
      return values.Contains(lnWeek);
  }
  </code></pre>
  
  <p>And called it like this:</p>
  
  <pre><code>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
         });
  </code></pre>
  
  <p>Except I can't get this to work either. It gives the following error:</p>
  
  <pre><code>The type arguments for method 'DateTimeExtensions.In&lt;T&gt;(DateTime, IEnumerable&lt;int&gt;)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
  </code></pre>
  
  <p>Can someone help please?</p>