2012-12-17 31 views
0

我在vs2012中使用EF 5。我正在使用DataContext並用一些linq表達式在Iqueryable中搜索。我想搜索一個EF iQueryable Linq不適用於十進制值比較

var res= dbContext.Products.OrderBy(s => s.Id).AsQueryable(); 
if (startPrice > -1 && endPrice > -1 && (startPrice < endPrice)) 
{ 
    res = res.Where(s => (s.UnitPrice >= startPrice && s.UnitPrice <= endPrice)); 
} 

var list= res.ToList(); 

這裏startPrice,endPrice,UnitPrice都是十進制類型。 我也添加了這個查詢的幾個條件。一切正常。但是這個比較(價格)返回一個空白的json數據。我已經嘗試與decimal.Compare()方法,但沒有運氣。你可以在這方面幫助我嗎?我正在學習linq。但沒有找到解決辦法。如果可能的話給一些建議:在谷歌搜索這種解決方案的是什麼?

+0

你檢查生成的SQL? –

+0

是的。它增加了比較。但不能在運行時工作。當我打電話給ToList()方法 –

+0

你可以檢查startPrice和endPrice是否正確?您可以檢查硬編碼值以進行測試。我已經厭倦了你的榜樣,它對我來說工作正常 –

回答

0

這裏我使用Northwnd示例數據庫

decimal? startPrice = Convert.ToDecimal(400.5100); 
    decimal? endPrice = Convert.ToDecimal(500.5100); 

    var res = NDC.Orders.OrderBy(s => s.OrderID).AsQueryable();       
       if (startPrice > -1 && endPrice > -1 && (startPrice < endPrice)) 
       { 
        res = res.Where(s => (s.Freight >= startPrice && s.Freight <= endPrice)); 
       } 

       var l = res.ToList(); 

    foreach (Order dcl in l) 
       { 
        Response.Write(dcl.Freight.ToString()); 
        Response.Write("<br/>"); 
       } 

and result is 
458.7800 
477.9000 
487.3800 
411.8800 
424.3000 
487.5700 
400.8100