简体   繁体   English

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

[英]Retrieving Data from database within the last 7 days using linq

I have a view with data and I need to retrieve data only from the last 7 days. 我有一个数据view ,我只需要从过去7天检索数据。 I know there is a function for this if I was using sql query. 我知道如果我使用sql查询,有一个函数。 but I'm using Linq. 但我正在使用Linq。

here is my code: 这是我的代码:

                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);
                }

can anyone tell me how I do this in Linq ? 任何人都能告诉我我在Linq如何做到这一点?

You can use DateTime.Now.AddDays(-7) to get the record seven day older then current date. 您可以使用DateTime.Now.AddDays(-7)获取比当前日期早7天的记录。 Or you can use Datime.Today.AddDays(-7) if you want the time part and start from after 12 PM. 或者你可以使用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
                         });

Try this: 尝试这个:

    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
                         });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM