创建一个时间跨度为24/7的日期时间(每天重复)

时间:2014-01-24 08:14:42

标签: c# asp.net-mvc linq datetime nopcommerce

我做了一个List方法,它可以获取我的MVC / ASP.NET应用程序中已售产品的总量。我需要在List方法中添加一个DateTime参数,告诉它只能获得前一天的销售产品。

此列表的目的是客户可以看到他前一天销售的产品和销售产品的数量。这只是一些销售统计数据。

我不确定如何为获取项目的方法创建时间跨度。我正在考虑使用datetime作为List方法的参数,然后声明时间跨度。我不知道如何解决这个问题..

这是我的IList方法:

public IList<BestsellersReportLine> DailyBestSellersReport(int billingCountryId = 0,
            int recordsToReturn = 5, int orderBy = 1, int groupBy = 1, bool showHidden = false)
        {


                var query1 = from opv in _opvRepository.Table
                         join o in _orderRepository.Table on opv.OrderId equals o.Id
                         join pv in _productVariantRepository.Table on opv.ProductVariantId equals pv.Id
                         join p in _productRepository.Table on pv.ProductId equals p.Id
                         select opv;


                var query2 = groupBy == 1 ?
                    //group by product variants
                       from opv in query1
                       group opv by opv.ProductVariantId into g
                       select new
                       {
                           EntityId = g.Key,
                           TotalAmount = g.Sum(x => x.PriceExclTax),
                           TotalQuantity = g.Sum(x => x.Quantity),
                       }
                       :
                    //group by products
                       from opv in query1
                       group opv by opv.ProductVariant.ProductId into g
                       select new
                       {
                           EntityId = g.Key,
                           TotalAmount = g.Sum(x => x.PriceExclTax),
                           TotalQuantity = g.Sum(x => x.Quantity),
                       }
                       ;

                switch (orderBy)
                {
                    case 1:
                        {
                            query2 = query2.OrderByDescending(x => x.TotalQuantity);
                        }
                        break;
                    case 2:
                        {
                            query2 = query2.OrderByDescending(x => x.TotalAmount);
                        }
                        break;
                    default:
                        throw new ArgumentException("Wrong orderBy parameter", "orderBy");
                }

                if (recordsToReturn != 0 && recordsToReturn != int.MaxValue)
                    query2 = query2.Take(recordsToReturn);

                var result = query2.ToList().Select(x =>
                {
                    var reportLine = new BestsellersReportLine()
                    {
                        EntityId = x.EntityId,
                        TotalAmount = x.TotalAmount,
                        TotalQuantity = x.TotalQuantity
                    };
                    return reportLine;
                }).ToList();

                return result;

        }

我已经创建了这些变量,如果这是正确的话dunno?:

        DateTime CurrentDay = DateTime.Now;
        DateTime PreviousDay = DateTime.Now.AddDays(-1);

        DateTime TimeFrame = DateTime.Parse("00:00:00 AM");

Anny想法?

谢谢!

//克里斯

3 个答案:

答案 0 :(得分:1)

var yesterday = DateTime.Now.Subtract( new TimeSpan( 1, 0, 0, 0 ) );
var earliest = new DateTime( yesterday.Year, yesterday.Month, yesterday.Day, 0, 0, 0 );
var latest = earliest.Add( new TimeSpan( 1, 0, 0, 0, -1 ) );

用法;

where earliest <= dateToTest && latest >= dateToTest

答案 1 :(得分:1)

你可以使用,

 var range = new
        {
            Start = DateTime.Today.AddDays(-1),
            End = DateTime.Today.AddSeconds(-1)
        };

并使用它,

(DateOfSale>=range.Start && DateOfSale<=range.End)

答案 2 :(得分:1)

答案很简单,您可以在上午12点计算日期,然后继续操作,

   DateTime Today12AMDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
   DateTime PreviousDay12AMDate = Today12AMDate.AddDays(-1);

你的情况会是这样的,

where dateFromDatabase <= Today12AMDate && dateFromDatabase >= PreviousDay12AMDate