Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 在linq表达式中创建新对象时,获取列表中的当前索引位置_C#_Linq_Automapper_Mapper - Fatal编程技术网

C# 在linq表达式中创建新对象时,获取列表中的当前索引位置

C# 在linq表达式中创建新对象时,获取列表中的当前索引位置,c#,linq,automapper,mapper,C#,Linq,Automapper,Mapper,创建新对象时,从LINQ列表访问当前位置时遇到问题 我试图声明一个变量并在新对象中递增它,但没有结果。我没有其他想法。在linq表达式中搜索一段时间,但找不到答案 var mapper = Singleton.CreateMapper<DocOutput, List<Document>>(); mapper.AddMapping(r => r.Docs, i => i, (x) => new Document() { Id = x.Elemen

创建新对象时,从LINQ列表访问当前位置时遇到问题

我试图声明一个变量并在新对象中递增它,但没有结果。我没有其他想法。在linq表达式中搜索一段时间,但找不到答案

var mapper = Singleton.CreateMapper<DocOutput, List<Document>>();
mapper.AddMapping(r => r.Docs, i => i, (x) => new Document() 
{
    Id = x.ElementAt( Index Position ).Extrato1.Split('.')[0],
}
var-mapper=Singleton.CreateMapper();
AddMapping(r=>r.Docs,i=>i,(x)=>newdocument()
{
Id=x.ElementAt(索引位置).Extrato1.Split('.')[0],
}
在索引位置,我需要当前对象的位置


提前感谢,我们将非常感谢您的帮助。

如果我正确理解了您的问题,您就有了
列表
对象,您需要浏览该列表,为创建其他对象的每个步骤建立索引。
返回值是一个映射(我在这里使用dictionary)-IEnumerable of
Tuple

为什么不直接使用
select
创建元组,如下所示:

var mapper = new List<Document>();

var mapping = mapper.
                  Select((doc, n) => new System.Tuple<int, Document>(n, doc)); 

让我知道这是您需要的。

您使用的是什么映射库?您好,这是一个基于元组的内部映射器解决方案
var mapper = new List<Document>();

var mapping = mapper.
                  Select((doc, n) => 
                         new System.Tuple<int, Document>(doc.ElementAt(n).Extrato1.Split('.')[0], doc));