1

感谢所有回答我最后一个问题并做出贡献的人。我遇到了另一个有趣的 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 属性来索引。

4

1 回答 1

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);

我故意将枚举列表的构成分开 - 为清楚起见 - 如果你愿意,你可以将它移到一行中。

于 2013-03-27T01:35:07.707 回答