簡體   English   中英

無法弄清楚如何在此LINQ查詢中添加where子句

[英]can't figure out how to add where clause to this LINQ query

我仍在嘗試使用LINQ,並想知道是否有人可以闡明我在這里做錯了什么。 我要從數據庫中拉出一排行,然后按ManufacturerSKU分組(ManufacturersSKU的多行具有不同的ListingPrice,並且我從數據庫中獲得了價格最便宜的ManufacturerSKU)

//store the cheapest row for a ManufacturerSKU in a list
var product_result = (
    from row in dtProductListings.AsEnumerable()
    group row by row.Field<string>("ManufacturerSKU") into g
    select new Product
    {
        ManufacturerSKU = g.Key,
        ListingPrice = g.Min(x => x.Field<double>("ListingPrice")),
        Manufacturer = g.Min(z => z.Field<string>("Manufacturer")),
    }).ToList();

無論如何,我一直在嘗試向查詢中添加where子句,以便可以使用mysql數據庫中的“ TimeStamp”列在某個日期范圍內獲取結果。 我嘗試添加

where row.Field< what data type even goes in here for a date time? >("TimeStamp") >= etc..

但是我知道我一定做錯了,因為它告訴我在這種情況下行不存在。 我嘗試在select后面的group子句之前添加where子句,但沒有成功。 有人能提供任何建議嗎?

(編輯:時間戳是一個日期時間)

假設您將dateFromdateTo變量作為日期范圍的邊界。 由於TimeStamp列的類型為datetime,因此應使用row.Field<DateTime>("TimeStamp") ,並且where子句應位於fromgroup之間,如下所示

var dateFrom = new DateTime(2014, 11, 1);
var dateTo = new DateTime(2014, 11, 30);

var product_result = (
    from row in dtProductListings.AsEnumerable()
    where row.Field<DateTime>("TimeStamp") > dateFrom 
    && row.Field<DateTime>("TimeStamp") < dateTo
    group row by row.Field<string>("ManufacturerSKU") into g
    select new Product
    {
        ManufacturerSKU = g.Key,
        ListingPrice = g.Min(x => x.Field<double>("ListingPrice")),
        Manufacturer = g.Min(z => z.Field<string>("Manufacturer")),
    }).ToList();

上面的代碼將檢索2014年11月1日至2014年11月30日之間的時間TimeStamp記錄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM