2012-09-03 40 views
2

下午空, 我似乎無法工作,如何忽略,如果下面一行是空您在我的LINQ查詢

where ((double)t.twePrice >= priceFrom && (double)t.twePrice <= (priceTo)) 

正如你可以在我的代碼見下面我能夠做到這一點的時候它是一個字符串。但我在價格上有問題。有人可以幫我解釋一下嗎?

from a in dc.aboProducts 
join t in dc.tweProducts on a.sku equals t.sku 
where (string.IsNullOrEmpty(productSku) || productSku == t.sku) 
where (string.IsNullOrEmpty(productAsin) || productAsin == a.asin) 
where (string.IsNullOrEmpty(productName) || t.title.Contains(productName)) 
where (string.IsNullOrEmpty(productBrand) || t.brand.Contains(productBrand)) 
where ((double)t.twePrice >= priceFrom && (double)t.twePrice <= (priceTo)) 
select new GetProducts 

更新:我基本上是在發送項目搜索我的MS SQL數據庫的負載,其中有些可能是NULL作爲每串。這些價格也可能是空的,因爲並不是一直需要。所以如果價格爲零,我不需要使用它們。

非常感謝。

+0

你的意思是說「包含pricefrom」和「priceTo」可以爲空,在這種情況下,你想忽略這個查詢? –

+0

是的,他們可以爲空,如果他們與其他選項一樣需要被忽略:) – thatuxguy

回答

0

也許下面可能就足夠了:

where ((priceFrom == null || (double)t.twePrice >= priceFrom) && (priceTo == null || (double)t.twePrice <= priceTo)) 

對於可讀性它可能在兩個破發:

where (priceFrom == null || (double)t.twePrice >= priceFrom) 
where (priceTo == null || (double)t.twePrice <= priceTo) 
+0

謝謝,但這不起作用,我更新了我的問題,因爲我剛注意到我錯過了一些重要細節! – thatuxguy

+0

我的陳述的第二部分是否不包含它? –

+0

否:(該tweprice永遠不會爲空。只有priceFrom和priceTo可能是因爲它們被傳遞給查詢 – thatuxguy

0

檢查twePrice不爲空,例如:

where (t.twePrice != null && (double)t.twePrice >= priceFrom && (double)t.twePrice <= (priceTo)) 

@thatuxguy,我猜你需要一些像這樣的事情。在twePrice爲空的情況下,下面的代碼忽略條件。

where (t.twePrice == null ? true : (double)t.twePrice >= priceFrom && (double)t.twePrice <= (priceTo)) 

祝您好運!