我有一个很大的嵌套来选择新的类,效果很好,但我想在"TestDictionary“中动态添加一个新的KeyValuePair,它的值来自"B”。
from a ... from b ...
select new c
{
TestC = b.Foo,
TestDictionary = new Dictionary(b.Parameters.ToDictionary(
x => x.ParameterKey, x => (object)x.ParameterValue))
// .Add(new.. "SomeKey", b.SomeValue) ?
}).ToList()
尝试使用Add方法,但无法使其工作。可以在新创建的字典中动态添加一个keyvaluepair吗?
提前感谢
/Lasse
发布于 2011-12-21 13:46:49
您应该能够使用集合初始值设定项语法。当Add()
方法接受多个参数时,如使用Dictionary
,请将每个参数都用大括号括起来:
from a ... from b ...
select new c
{
TestC = b.Foo,
TestDictionary = new Dictionary(b.Parameters.ToDictionary(
x => x.ParameterKey, x => (object)x.ParameterValue))
{
{"SomeKey", b.SomeValue},
//{"SomeOtherKey", b.FooBar} //if you had more than one…
};
}).ToList()
https://stackoverflow.com/questions/8590646
复制相似问题