.ToDictionary(t => t.Key, t => t.Value) を使用して linq 結果セットを Dictionary コレクションに変換できることはわかっていますが、それ以上のものを探しています。指定されたlinq結果をに変換したいのですIEnumerable<Dictionary<string, object>>
が、これが私が探しているものです:
これが私のlinqクエリです:
var query = from v in dbContext.Persons
where !v.InActive
select new
{
v.PersonId,
v.Name,
v.DateOfBirth,
};
this.Persons = query.ToDictionaryCollection();
ToDictionaryCollection は次のようになります。
public static IEnumerable<Dictionary<string, object>> ToDictionaryCollection<T>(this IEnumerable<T> collection) where T : class
{
if (collection == null || collection.Count() == 0)
{
return new List<Dictionary<string, object>>();
}
Type givenType = collection.First().GetType();
PropertyInfo[] properties = givenType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
return collection
.Select(entity =>
{
return properties.Select(prop => new { Key = prop.Name, Value = prop.GetValue(entity) }).ToDictionary(prop => prop.Key, prop => prop.Value);
});
}
現在の実装では、すべてのエンティティでリフレクションを使用するとペナルティが発生すると思います。ラムダ式ツリーなどを使用して、これを行うより良い方法はありますか?
注: 上記のコードは、Windows Phone 8 および Windows Store 8.1 アプリケーション用です。
ありがとう、ビノイ