我有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中的表现吗?
答案 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
});