オブジェクト内の列挙可能なプロパティを識別し、それを Dictionary オブジェクトに変換しようとしています
リストのリストをリストに変換するためにラムダ式を使用してlinqクエリを作成し、このmsdn記事の例に従っています
LINQPad で次のプログラムを実行しようとすると、コンパイル時エラーが発生します
void Main()
{
var list = new List<int>();
list.Add(1);
list.Add(2);
var list2 = new List<string>();
list2.Add("ab");
list2.Add("xy");
var obj = new { x = "hi", y = list, z = list2 , a =1};
var properties = (obj.GetType()).GetProperties()
.Select(x => new {name =x.Name , value= x.GetValue(obj, null)})
.Where( x=> x.value != null && (x.value is IEnumerable) && x.value.GetType() != typeof(string) )
.Select(x => new {name = x.name, value= x.value});
Console.WriteLine(properties);
foreach( var item in properties)
{
var col = (IEnumerable) item.value;
foreach ( var a in col)
{
Console.WriteLine("{0}-{1}",item.name,a);
}
}
//compile time error in following line
var abc = properties.SelectMany(prop => (IEnumerable )prop.value, (prop,propvalue) => new {prop,propvalue} )
.Select( propNameValue =>
new {
name = propNameValue.prop.name,
value = propNameValue.propvalue
}
);
Console.WriteLine(abc);
}
メソッド 'System.Linq.Enumerable.SelectMany(System.Collections.Generic.IEnumerable, System.Func>, System.Func)' の型引数は、使用法から推測できません。型引数を明示的に指定してみてください。
SelectMany ステートメントを再構築してエラーを取り除き、ネストされた foreach ループと同様の出力を得るにはどうすればよいですか?