Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 选择列表<;字符串>;用索引输入词典_C#_List_Dictionary - Fatal编程技术网

C# 选择列表<;字符串>;用索引输入词典

C# 选择列表<;字符串>;用索引输入词典,c#,list,dictionary,C#,List,Dictionary,我有一张单子 List<string> sList = new List<string>() { "a","b","c"}; List sList=newlist(){“a”、“b”、“c”}; 目前,我正在将其编入字典,其结构如下: //(1,a)(2,b)(3,c) Dictionary<int, string> dResult = new Dictionary<int, string>(); for(int i=0;i< sList

我有一张单子

List<string> sList = new List<string>() { "a","b","c"};
List sList=newlist(){“a”、“b”、“c”};
目前,我正在将其编入字典,其结构如下:

//(1,a)(2,b)(3,c)
Dictionary<int, string> dResult = new Dictionary<int, string>();
for(int i=0;i< sList.Count;i++)
{
    dResult.Add(i, sList[i]);
}
/(1,a)(2,b)(3,c)
Dictionary dResult=新字典();
for(int i=0;i
但是对于Linq,我看到了一些更精简的方式,比如
ToDictionary((x,index)=>


如何使用正确的语法或如何在一行内解决此问题?

您可以使用
Select
的重载来投影索引以填充匿名类型:

Dictionary<int, string> dResult = sList
    .Select((s, index) => new { s, index })
    .ToDictionary(x => x.index, x => x.s);
Dictionary<int, string> dResult = Enumerable
    .Range(0, sList.Count())
    .ToDictionary(i => i + 1, i => sList[i]);
字典dResult=sList
.Select((s,index)=>new{s,index})
.ToDictionary(x=>x.index,x=>x.s);

这与您的代码所做的相同。如果您希望得到您已注释的结果:
(1,a)(2,b)(3,c)
)您必须添加+1,因此
到字典(x=>x.index+1,x=>x.s)
首先您必须使用提供当前使用元素索引的

var tmp = sList.Select((x, i) => new { Key = i, Value = x });
现在创建字典:

var result = tmp.ToDictionary(x => x.Key, x => x.Value);
或作为一个班轮:

var result = sList.Select((x, i) => new { Key = i, Value = x }).ToDictionary(x => x.Key, x => x.Value);
编辑:您还可以创建自己的扩展方法,以使用您提供的语法:

public static Dictionary<TResultKey, TResultValue> ToDictionary<TSource, TResultKey, TResultValue>(this IEnumerable<TSource> src, Func<TSource, int, TResultKey> KeySelector, Func<TSource, int, TResultValue> ValueSelector) 
{
    var result = new Dictionary<TResultKey, TResultValue>();
    int i = 0;
    foreach (var element in src)
    {
        result[KeySelector(element, i)]  = ValueSelector(element, i);
        i++;
    }
    return result;
}
public static Dictionary to Dictionary(此IEnumerable src、Func KeySelector、Func ValueSelector)
{
var result=newdictionary();
int i=0;
foreach(src中的var元素)
{
结果[键选择器(元素,i)]=值选择器(元素,i);
i++;
}
返回结果;
}
现在你可以这样称呼它:

List<string> sList = new List<string>() { "a","b","c"};
var result = sList.ToDictionary((x, i) => i + 1, (x, i) => x);
List sList=newlist(){“a”、“b”、“c”};
var结果=sList.ToDictionary((x,i)=>i+1,(x,i)=>x);

没有
ToDictionary
方法在单个调用中提供此功能,因为没有提供索引的重载,但您可以使用接受
函数的
Select
重载:

或者在C#7中,避免使用
ValueTuple
创建一堆堆分配的对象:

var dictionary = list.Select((v, i) => (value: v, index: i))
                     .ToDictionary(pair => pair.index, pair => pair.value);
从C#7.1 point发行版开始,您可以使用推断的
ValueTuple
literal表达式:

var dictionary = list.Select((value, index) => (value, index))
                     .ToDictionary(pair => pair.index, pair => pair.value);

(正如Tim所指出的,您需要将索引调整1以获得原始请求的输出。)

您也可以在不使用匿名类型的情况下使用此方法:

Dictionary<int, string> dResult = sList
    .Select((s, index) => new { s, index })
    .ToDictionary(x => x.index, x => x.s);
Dictionary<int, string> dResult = Enumerable
    .Range(0, sList.Count())
    .ToDictionary(i => i + 1, i => sList[i]);
Dictionary dResult=可枚举
.Range(0,sList.Count())
.ToDictionary(i=>i+1,i=>sList[i]);

您知道原始列表的性能会比字典更好吗?例如,当你有稀疏或非数字键时,通常使用字典。这是否回答了你的问题?