2016-04-05 18 views
0

我正在嘗試爲我的C#表單使用linq來計算所選字段的總數量。但是,這些字段可以按stor_id或ord_num排序。任何時候我嘗試編寫一條或一條語句來獲取我的查詢中的總數,我都會遇到語法錯誤。當我只查詢stor_id或ord_num時,它工作正常,只是將它們組合起來導致問題。這就是我的查詢現在的樣子。我怎樣才能得到一個數據表中的排序字段的總數量與lnq?

System.Nullable<double> total = (from ord in _Pubs_1_DataSet.sales where 
           (ord.ord_num == txtOrderNumber.Text 
           or ord.stor_id == txtStoreID.Text) 
           select (int)ord.qty).Sum(); 

回答

0

or不是這樣。只要使用||(這基本上意味着or

System.Nullable<double> total = (from ord in _Pubs_1_DataSet.sales where (ord.ord_num == txtOrderNumber.Text || ord.stor_id == txtStoreID.Text) select (int) ord.qty).Sum(); 
+0

這很有道理,我試圖寫它像一個SQL查詢,而不是與C#邏輯。感謝你及時的答覆 –

0

||是在C#or。我還在查詢中包含了ord.qty.HasValue來過濾nullable數量。

(from ord in _Pubs_1_DataSet.sales 
where ((ord.ord_num == txtOrderNumber.Text || ord.stor_id == txtStoreID.Text) && ord.qty.HasValue) 
select ord.qty.Value).Sum(); 
相關問題