2016-09-04 22 views
0

我有一個查詢選擇某個值。該代碼是:在開關櫃中添加Linq條件值

var query = from c in snd.external_invoices.OrderByDescending(x => x.date) 
      join o in snd.invoices on c.idexternal_invoices equals o.id_external_invoice 
      select new 
        { 
         c.idexternal_invoices, 
         c.businessname, 
         o.number, 
         c.message, 
         c.price, 
         c.date, 
         c.tipologiaPagamento, 
         c.esitoPagamento, 
         c.iduser 
        }; 

現在我需要選擇基於在該領域c.tipologiaPagamento值這個查詢的一些價值。特別是,我需要選擇所有地方c.date它等於該開關結果值:

  switch (c.tipologiaPagamento) 
      { 
       case "1": 
        c.date.AddDays(10); 
        break; 
       case "2": 
        c.date.AddDays(10); 
        break; 
       case "3": 
        DateTime endOfMonth = new DateTime(c.date.Year, 
            c.date.Month, 
            DateTime.DaysInMonth(c.date.Year, 
                 c.date.Month)); 
        c.date = endOfMonth; 
        break; 
       case "4": 
        DateTime nextMonth = c.date.AddMonths(1); 
        DateTime endOfNextMonth = new DateTime(c.date.Year, 
            c.date.Month, 
            DateTime.DaysInMonth(c.date.Year, 
                 c.date.Month)); 
        c.date = endOfNextMonth; 
        break; 
       default: 
        break; 
      } 

所以我需要在查詢中選擇其中c.date等於該開關的結果值。

我該怎麼做?

感謝所有

回答

0

我不知道,你可以在一個複雜的計算傳似歐想要輕鬆放入LINQ表達?如果我錯了,有人可以解決我的問題。

我可能會這樣做,因爲您的計算僅取決於您的tipologiaPagamento值,並且僅在計算中使用date值,而您在當前結果集中使用該值,不要在SQL上執行該操作側。只需獲取所有需要的信息,並將您的switch語句放入foreach循環中,根據需要更新應用程序服務器級別的值。這可能會比SQL服務器更快地完成計算,SQL服務器不一定針對數據操作進行了優化,而更多用於數據檢索。

+0

唯一的問題是匿名類型是不可變的,因此無法進行更新。 –

+0

是的,這是正確的,您需要爲數據創建一個類型來執行此操作。謝謝@IvanStoev –

0

試試這個:

var query = from c in snd.external_invoices.OrderByDescending(x => x.date) 
         join o in snd.invoices on c.idexternal_invoices equals o.id_external_invoice 
         where c.date== FindDate(c.tipologiaPagamento, c.date) 
         select new 
         { 
          c.idexternal_invoices, 
          c.businessname, 
          o.number, 
          c.message, 
          c.price, 
          c.date, 
          c.tipologiaPagamento, 
          c.esitoPagamento, 
          c.iduser 
         }; 



    public DateTime FindDate(string tipologiaPagamento, DateTime date) 
     { 

      DateTime Result = new DateTime(); 
      switch (tipologiaPagamento) 
      { 
       case "1": 
        Result = date.AddDays(10); 
        break; 
       case "2": 
        Result = date.AddDays(10); 
        break; 
       case "3": 
        DateTime endOfMonth = new DateTime(date.Year, 
            date.Month, 
            DateTime.DaysInMonth(date.Year, 
                 date.Month)); 
        date = endOfMonth; 
        break; 
       case "4": 
        DateTime nextMonth = date.AddMonths(1); 
        DateTime endOfNextMonth = new DateTime(date.Year, 
            date.Month, 
            DateTime.DaysInMonth(date.Year, 
                 date.Month)); 
        date = endOfNextMonth; 
        break; 
       default: 
        break; 
      } 
      return Result; 

     } 
0

這不是很清楚,但很有效:

from c in ... 
... 
select new 
    { 
    Invoices = c.idexternal_invoices, 
    Cdate = c.tipologiaPagamento == 1? c.date 
      : c.tipologiaPagamento == 2? c.date.AddDays(10) 
      : c.tipologiaPagamento == 3? new DateTime(2015,9,1) 
      : new DateTime(1970,1,1), 
    User = c.iduser 
}; 

...因爲它可以被編譯LINQ表達式樹,而不是在C#中端執行的一部分。