2

朝、LINQで(MS)SQLのこのビットを実行する方法を知りたいです...

 SELECT CONVERT(CHAR(10),orderDate,110)  AS OrderDate, 
 SUM(1) AS TotalOrders 
 FROM Orders
 WHERE OrderDate>getdate()-30
 GROUP BY CONVERT(CHAR(10),orderDate,110)
 ORDER BY OrderDate DESC

よろしくお願いします。

更新 私はそれを共有すると思って、以下に提供されているソリューションの使用と編集バージョンの使用を終了しました...

 using (DataDataContext dc = new DataDataContext())
        {
            var query = from o in dc.Orders
                        where o.orderDate > DateTime.Now.AddDays(-30)
                        let dt = o.orderDate
                        group o by new DateTime(dt.Year, dt.Month, dt.Day) into g
                        select new OrderCounts
                        {
                            OrderDate = String.Format("{0:d}", g.Key.Date),
                            TotalOrders = g.Count()
                        };

            query.GroupBy(o => o.OrderDate);
            query.OrderBy(o => o.OrderDate);

            return query.ToList();
        }
4

2 に答える 2

2
var result =(from s in Orders
             where s.OrderDate > DateTime.Now.AddDays(-30)
             group s by new { date = s.OrderDate.ToString() } into g
             // or use ((DateTime)s.OrderDate).ToShortDateString() instead of s.OrderDate.ToString()
             select new
             {
                  OrderDate = g.Key.date,
                  TotalOrders = g.Count()
             }).OrderByDescending(x=>x.OrderDate);;
于 2012-07-27T09:03:00.533 に答える
1

クエリは次のようになります

var query = from o in res
                        where o.OrderDate > DateTime.Now.AddDays(-30)
                        orderby o.OrderDate descending
                        group o by o.OrderDate into g                       
                        select new
                        {
                            OrderDate = g.Key.Date.ToString("MM/dd/yyyy")
                            ,
                            TotalOrders = g.Sum(i=> 1)
                        };

またはラムダ式による

var query= res
                        .Where(i => i.OrderDate > DateTime.Now.AddDays(-30))
                        .OrderByDescending(o => o.OrderDate)
                        .GroupBy(g => g.OrderDate)
                        .Select(s => new { OrderDate = s.Key.Date.ToString("MM/dd/yyyy"), TotalOrders = s.Sum(i => 1) });
于 2012-07-27T09:45:03.717 に答える