简体   繁体   English

C#Linq查询返回类型到字典

[英]C# Linq query return type to dictionary

I have a linq query below. 我在下面有一个linq查询。 I have a similar query in 3 other places which is obviously not great. 我在其他3个地方也有类似的查询,这显然不是很好。 So I want to pass the tfID to a method. 所以我想将tfID传递给方法。 I know you cannot pass var as a parameter to a method. 我知道您不能将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 }; 

I wanted to change the query above to something like below. 我想将上面的查询更改为下面的内容。 However error message I get is "cannot complicity convert type 'System.Collections.Generic.IEnumerable to 'System.Collections.Generic.Dictionary' An explicit conversion exists (are you missing a cast). 但是,我得到的错误消息是“无法将类型'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 };

Use ToDictionary 使用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);
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);

You can use the ToDictionary() method to turn the result into a dictionary. 您可以使用ToDictionary()方法将结果转换为字典。 Ie

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);

I know you cannot pass var as a parameter to a method. 我知道您不能将var作为参数传递给方法。

Not true - var is not (necessarily) an anonymous type - it just lets the compiler decide what the type is. 不是正确的var不是(不一定)是匿名类型-它只是让编译器确定类型是什么。

All you need to do is add ToDictionary() : 您需要做的只是添加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 will then be a Dictionary<Order, AssetDetail> 然后tfID将是Dictionary<Order, AssetDetail>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM