7

我有一个view数据,我只需要检索过去 7 天的数据。如果我使用 sql 查询,我知道有一个功能。但我正在使用 Linq。

这是我的代码:

                try
                {
                var query = (from bwl in mg.BarcodeWithLocation
                             select new
                             {
                                 RequestID = bwl.RequestID,
                                 Barcode = bwl.Barcode,
                                 adrid = bwl.AdrID,
                                 name = bwl.Name,
                                 street = bwl.Street,
                                 houseno = bwl.HouseNo,
                                 postal = bwl.Postal,
                                 city = bwl.City,
                                 country = bwl.Country,
                                 latitudetxt = bwl.Latitude == "" ? "Location Unknown" : "View Map Location",
                                 latitude = bwl.Latitude,
                                 longitude = bwl.Longitude,
                                 date = bwl.ReceivedDate
                             });

                this.Grid.DataSource = query;
                this.Grid.DataBind();
                }
                catch (Exception exception)
                {
                      Console.WriteLine("ERROR in GetNoLocationScan() method. Error Message : " + exception.Message);
                }

谁能告诉我如何在 Linq 中做到这一点?

4

2 回答 2

14

您可以使用 DateTime.Now.AddDays(-7)获取比当前日期早 7 天的记录。或者您可以使用 Datime.Today.AddDays(-7) 如果您想要时间部分并从下午 12 点之后开始。

var query = (from bwl in mg.BarcodeWithLocation
              where(bwl.ReceivedDate > DateTime.Now.AddDays(-7))
                         select new
                         {
                             RequestID = bwl.RequestID,
                             Barcode = bwl.Barcode,
                             adrid = bwl.AdrID,
                             name = bwl.Name,
                             street = bwl.Street,
                             houseno = bwl.HouseNo,
                             postal = bwl.Postal,
                             city = bwl.City,
                             country = bwl.Country,
                             latitudetxt = bwl.Latitude == "" ? "Location Unknown" : "View Map Location",
                             latitude = bwl.Latitude,
                             longitude = bwl.Longitude,
                             date = bwl.ReceivedDate
                         });
于 2012-11-06T09:48:43.127 回答
4

试试这个:

    var dt = DateTime.Now.AddDays(-7);
    var query = (from bwl in mg.BarcodeWithLocation
                 where bwl.ReceivedDate > dt 
                         select new
                         {
                             RequestID = bwl.RequestID,
                             Barcode = bwl.Barcode,
                             adrid = bwl.AdrID,
                             name = bwl.Name,
                             street = bwl.Street,
                             houseno = bwl.HouseNo,
                             postal = bwl.Postal,
                             city = bwl.City,
                             country = bwl.Country,
                             latitudetxt = bwl.Latitude == "" ? "Location Unknown" : "View Map Location",
                             latitude = bwl.Latitude,
                             longitude = bwl.Longitude,
                             date = bwl.ReceivedDate
                         });
于 2012-11-06T09:49:41.723 回答