我在下面有一个linq查询。我在其他3个地方有类似的查询,这显然不是很好。所以我想将tfID传递给方法。我知道你不能将var作为参数传递给方法。
var tfID = from orderItem in _orderBlock.Orders
join assetItem in tfEntity.AssetDetails on orderItem.Security.ID equals assetItem.sISIN
where orderItem.IdenitfierUsed == "I"
where assetItem.iSkeletal == 1
select new { orderItem, assetItem };
我想将上面的查询更改为以下内容。但是,我得到的错误消息是“无法共谋转换类型”System.Collections.Generic.IEnumerable到'System.Collections.Generic.Dictionary'存在显式转换(您是否错过了转换)。
Dictionary<Order, AssetDetail> ordersDictionary = new Dictionary<Order, AssetDetail>();
ordersDictionary = from orderItem in _orderBlock.Orders
join assetItem in tfEntity.AssetDetails on orderItem.Security.ID equals assetItem.sISIN
where orderItem.IdenitfierUsed == "I"
where assetItem.iSkeletal == 1
select new { orderItem, assetItem };
答案 0 :(得分:3)
使用ToDictionary
Dictionary<Order, AssetDetail> ordersDictionary =
(from orderItem in _orderBlock.Orders
join assetItem in tfEntity.AssetDetails on orderItem.Security.ID equals assetItem.sISIN
where orderItem.IdenitfierUsed == "I"
where assetItem.iSkeletal == 1
select new { orderItem, assetItem })
.ToDictionary(x => x.orderItem, x => x.assetItem);
答案 1 :(得分:2)
ordersDictionary = (from orderItem in _orderBlock.Orders
join assetItem in tfEntity.AssetDetails on orderItem.Security.ID
equals assetItem.sISIN
where orderItem.IdenitfierUsed == "I"
where assetItem.iSkeletal == 1
select new { orderItem, assetItem }
).ToDictionary(x=>x.orderItem, x=>x.assetItem);
答案 2 :(得分:1)
您可以使用ToDictionary()方法将结果转换为字典。即。
Dictionary<Order, AssetDetail> ordersDictionary = new Dictionary<Order, AssetDetail>();
ordersDictionary = (from orderItem in _orderBlock.Orders
join assetItem in tfEntity.AssetDetails on orderItem.Security.ID equals assetItem.sISIN
where orderItem.IdenitfierUsed == "I"
where assetItem.iSkeletal == 1
select new { orderItem, assetItem }).ToDictionary(key => //the key, value => the value);
答案 3 :(得分:1)
我知道你不能将var作为参数传递给方法。
不正确 - var
不是(必然)匿名类型 - 它只是让编译器决定类型是什么。
您需要做的只是添加ToDictionary()
:
var tfID = (from orderItem in _orderBlock.Orders
join assetItem in tfEntity.AssetDetails on orderItem.Security.ID equals assetItem.sISIN
where orderItem.IdenitfierUsed == "I"
where assetItem.iSkeletal == 1
select new { orderItem, assetItem })
.ToDictionary(i => orderItem, i => assetItem);
tfID
将成为Dictionary<Order, AssetDetail>