Lah*_*hib 6 .net c# 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);
}
Run Code Online (Sandbox Code Playgroud)
任何人都能告诉我我在Linq如何做到这一点?
Adi*_*dil 11
您可以使用 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
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10251 次 |
| 最近记录: |