I have a view
with data and I need to retrieve data only from the last 7 days. I know there is a function for this if I was using sql query. but I'm using 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 ?
You can use DateTime.Now.AddDays(-7)
to get the record seven day older then current date. Or you can use Datime.Today.AddDays(-7) if you want the time part and start from after 12 PM.
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
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With