其中值是一个匿名类型在C#

其中值是一个匿名类型在C#

本文介绍了字典,其中值是一个匿名类型在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在C#创建一个 System.Collections.Generic.Dictionary< TKEY的,TValue> ,其中 TKEY的是无条件的阶级和 TValue - 匿名类的一些特性,例如 - 数据库列名和它的本地化名称

事情是这样的:

 新{n = 1,名称=新{列=美元,本地化=Доллар}}
 

解决方案

您不能声明这样一个字典类型直接(也有组装机,但这些只用于娱乐和新奇的目的),但如果你的数据是从未来的IEnumerable或IQueryable的源代码,你可以使用LINQ ToDictionary运营商和投射出所需要的密钥和序列元素(匿名类型)的价值得到一个:

  VAR intToAnon = sourceSequence.ToDictionary(
    E => e.Id,
    E =>新{e.Column,e.Localized});
 

Is it possible in C# to create a System.Collections.Generic.Dictionary<TKey, TValue> where TKey is unconditioned class and TValue - an anonymous class with a number of properties, for example - database column name and it's localized name.

Something like this:

new { ID = 1, Name = new { Column = "Dollar", Localized = "Доллар" } }
解决方案

You can't declare such a dictionary type directly (there are kludges but these are for entertainment and novelty purposes only), but if your data is coming from an IEnumerable or IQueryable source, you can get one using the LINQ ToDictionary operator and projecting out the required key and (anonymously typed) value from the sequence elements:

var intToAnon = sourceSequence.ToDictionary(
    e => e.Id,
    e => new { e.Column, e.Localized });

这篇关于字典,其中值是一个匿名类型在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 08:06