我有一個表'資產'和'ListView'(assetCategoriesListView)填充類別。Linq和ListView和包含
我想選擇與類別選擇所有資產:
Assets myAsset = dbLinqManager.db.Assets.Where(a => assetCategoriesListView.CheckedIndices.Contains(a.Category)).First();
但似乎包含不能使用。有誰知道解決方案?
我有一個表'資產'和'ListView'(assetCategoriesListView)填充類別。Linq和ListView和包含
我想選擇與類別選擇所有資產:
Assets myAsset = dbLinqManager.db.Assets.Where(a => assetCategoriesListView.CheckedIndices.Contains(a.Category)).First();
但似乎包含不能使用。有誰知道解決方案?
你被困在一個不合時宜的時代。 ListView類的日期從.NET 1.0開始,即泛型變得可用之前的黑暗日子。 CheckedIndices屬性的類型爲ListView.CheckedIndexCollection,它只實現IEnumerable,而不是IEnumerable <>。這就是爲什麼Contains()不起作用。
通常可以使用Cast()擴展方法修復它。但CheckedIndices包含整數,而不是對象。 CheckedItems包含ListViewItems。您計劃如何將它們映射到「資產」的方式並不明確。注重保持數據與視圖分離。
您可以使用'Contains',但只能使用簡單類型的列表:http://wekeroad.com/2008/02/27/creating-in-queries-with-linq-to-sql/ – mellamokb 2012-04-17 00:59:42
什麼你有錯誤嗎? – SLaks 2012-04-17 01:04:13