感谢所有回答并为我的上一个问题做出贡献的人们。我遇到了一个LINQ问题的另一个有趣的化身......就像之前......
我有以下
const String A_CONVERSATION = "AD Channels";
const String NOT_REPUTABLE = "AD Resolution";
const String DO_NOT_KNOW = "Capture Input";
private enum Properties
{ MyHow, Thats, It }
List<String> MyList = new List<string>
{
A_CONVERSATION,
NOT_REPUTABLE,
DO_NOT_KNOW
}
private Dictionary <Properties, String> PropertyToString;
private Dictionary <String, Properies> StringToProperty;
如何使用LINQ填充每个词典,以便我可以使用以下内容?是否有一行LINQ语句可以填充每个语句?
Properties MyResult1 = StringToProperty[A_CONVERSATION];
String MySResult2 = PropertyToString[Properties.It];
我特别想在第二种情况下使用actaull属性进行索引。
答案 0 :(得分:0)
提供这是你想要的......
在两个陈述中(一个可以合并,我猜想通过某种方式进行某种查找 - 但我认为不是那么相关 - 并非一切都必须在一行:)
var properties = ((Properties[])Enum.GetValues(typeof(Properties))).ToList();
var propertyToString = properties.Zip(MyList, (p, s) => new { Prop = p, Text = s }).ToDictionary(x => x.Prop, x => x.Text);
var stringToProperty = properties.Zip(MyList, (p, s) => new { Prop = p, Text = s }).ToDictionary(x => x.Text, x => x.Prop);
我故意将枚举列表中的内容分开 - 为清楚起见 - 如果您愿意,可以将其移动到一行。