2017-03-04 141 views
0

在linq查詢中選擇case語句。datediff with case statement in select query linq

這裏是SQL查詢:

select case when DATEDIFF(day,convert(varchar,Min([Order].CreatedOnUtc),101),convert(varchar,Max([Order].CreatedOnUtc),101)) = 0 then 
Sum([Order].OrderSubtotal) 
else 
case when (DATEDIFF(day,convert(varchar,Min([Order].CreatedOnUtc),101),convert(varchar,Max([Order].CreatedOnUtc),101))/30) = 0 then Sum([Order].OrderSubtotal) else 
Sum([Order].OrderSubtotal)/ 
    (DATEDIFF(day,convert(varchar,Min([Order].CreatedOnUtc),101),convert(varchar,Max([Order].CreatedOnUtc),101))/30) 
end 
end as 'Account Value' from [order] where And Account.ID = @Act_ID 

在這裏,我想代碼:

var query = _orderRepository.Table; 
     query = query.Where(o => o.AccountId == accountId); 

在查詢我收到我的價值。

查詢語句後應該怎麼寫? 如何使用linq編寫case語句?

+0

我希望下面的鏈接將有所幫助。 [linq-case-statement-stackoverflow](http://stackoverflow.com/questions/936028/linq-case-statement)[選擇案例在LINQ - Stackoverflow](http://stackoverflow.com/questions/ 4244023/select-case-in-linq) – Venu

+0

我已經看到這個聲明在stackoverflow ..但在我的情況下DATEDIFF與兩個case語句..所以你可以指導我?? – Manoj

+0

var query = _orderRepository.Table; query = query.Where(o => o.AccountId == accountId); var MinDate =(從查詢中選擇d.CreatedOnUtc).Min(); var MaxDate =(從查詢中的d選擇d.CreatedOnUtc).Max(); var rateSum =(query.Sum(d => d.OrderSubtotal)); 查詢= query.Where(C => (DbFunctions.DiffDays(爲MinDate,MAXDATE)== 0) 「rateSum」:???(DbFunctions.DiffDays(爲MinDate,MAXDATE)/ 30 == 0) 「rateSum」「 (rateSum /(DbFunctions.DiffDays(爲MinDate,MAXDATE)/ 30)「); – Manoj

回答

0

@Manoj,可能是下面的代碼可以幫助你。此示例C#項目可能會解決您的問題。

using System; 
using System.Collections.Generic; 
using System.Linq; 

namespace DateDiffIssue 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      // Preparing data 
      var data = new Order[] { 
       new Order { AccountID = 1, CreatedOnUtc = DateTime.Parse("1.01.2017 10:00"), OrderSubtotal = 100 }, 
       new Order { AccountID = 1, CreatedOnUtc = DateTime.Parse("1.01.2017 12:00"), OrderSubtotal = 150 }, 
       new Order { AccountID = 1, CreatedOnUtc = DateTime.Parse("1.01.2017 14:00"), OrderSubtotal = 150 } 
      }; 

      // Selection 
      var selected = (from item in data 
          let accountData = data.Where(w => w.AccountID == 1) 
          let minDate = accountData.Min(m => m.CreatedOnUtc).Date 
          let maxDate = accountData.Where(w => w.AccountID == 1).Max(m => m.CreatedOnUtc).Date 
          let isSameDate = minDate == maxDate 
          let basedOn30Days = (maxDate - minDate).TotalDays/30 
          let isInside30Days = (int)basedOn30Days == 0 
          let accountDataSum = accountData.Sum(s => s.OrderSubtotal) 
          select new 
          { 
           AccountValue = isSameDate ? accountDataSum : 
               isInside30Days ? accountDataSum : 
               accountDataSum/basedOn30Days 
          }).Distinct(); 

      // Print each order 
      selected.ToList().ForEach(Console.WriteLine); 

      // Wait for key 
      Console.WriteLine("Please press key"); 
      Console.ReadKey(); 
     } 
    } 

    internal class Order 
    { 
     public int AccountID { get; set; } 
     public DateTime CreatedOnUtc { get; set; } 
     public int OrderSubtotal { get; set; } 
    } 
} 
+0

超級..........? .. – Manoj