使用linq在过去7天内从数据库中检索数据

时间:2012-11-06 09:46:17

标签: c# .net linq entity-framework linq-to-sql

我有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中的表现吗?

2 个答案:

答案 0 :(得分:11)

您可以使用DateTime.Now.AddDays(-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
                         });

答案 1 :(得分:1)

试试这个:

    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
                         });
相关问题