![](/img/trans.png)
[英]c# - Linq Error “Operator '==' cannot be applied to operands of type 'int' and 'System.Linq.IQueryable<T>” and RemoveRange
[英]Operator '==' cannot be applied to operands of type 'int' and 'System.Linq.iQueryable<int>'
所以我有點卡住了! 做大學的編程工作,我碰壁了。
我們在其中一個部分提出的問題如下:
“2.允許用戶查找在特定年份下訂單的客戶。提供一個組合框,列出訂單表中用戶可以進行選擇的所有唯一(不同)年份。”
我在轉換“年份”時遇到問題,因此我可以將其與OrderID進行比較,並在列表框中顯示數據庫中的所有訂單。
如果有人能幫我一把,我將不勝感激! 謝謝!
這是我的代碼:
private void dateDDL_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string selection;
selection = dateDDL.SelectedItem.ToString();
var year = from y in northwind.Orders
where Convert.ToString(y.OrderDate).Contains(selection)
select y.OrderID;
var order = from o in northwind.Order_Details
where o.OrderID == year
select new { o.OrderID,
o.ProductID,
o.UnitPrice,
o.Quantity,
o.Discount };
lbxOrderdate.ItemsSource = order;
}
var year = (from y in northwind.Orders
where Convert.ToString(y.OrderDate).Contains(selection)
select y.OrderID).Single();
var order = from o in northwind.Order_Details
where o.OrderID == year
select new {
o.OrderID,
o.ProductID,
o.UnitPrice,
o.Quantity,
o.Discount
};
LINQ查詢還返回System.Linq.IQueryable<T>
,如果你想取單值,你應該使用Single()
/ First()
擴展名。
如果您不確定,該查詢結果包含對象,您應該FirstOrDefault()
或SingleOrDefault()
(當集合僅包含一個元素時, SingleOrDefault()
拋出異常default(T)
,如果沒有元素,則返回default(T)
結果。 請注意這一點。
您的year
查詢返回與您的where
子句匹配的所有OrderID
值的列表。 您無法將該列表與order
查詢中的單個o.OrderID
進行比較; 如果您想查找years
查詢返回的所有years
所有訂單,請使用類似的內容
where year.Contains(o.OrderID)
(未經測試,但應引導您走正確的路線)
類型非常重要,(除此之外)字符串的世界並不安全。
string selection = dateDDL.SelectedItem.ToString();
int year = Int32.Parse(selection);
var orders =
from order in northwind.Orders
where order.OrderDate.Year == year
select order;
“2.允許用戶查找在特定年份下訂單的客戶。提供一個組合框,列出訂單表中用戶可以進行選擇的所有唯一(不同)年份。”
嗯,查詢實際上不應該是關於客戶的嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.