使用linq查询时的行号

use*_*381 5 c# linq asp.net .net-4.0

我正在使用流阅读器来读取文本文件,然后使用Linq来检索信息

String fileContent = prodFileStreamReader.ReadToEnd();

 var mydata = from con in fileContent.Split('$').Select(x => x.Trim())
                    where !String.IsNullOrEmpty(con)
                    select new BaseSegment
                    {
                      dataID = con.Substring(0, con.IndexOf('#')),
                      dataElms = con.Split('#').ToArray(),
                      dataCon = con,
           lineNumber = 
                    };
Run Code Online (Sandbox Code Playgroud)

我还想得到行号.我尝试使用索引,但我无法做到.如何查询获取索引并将其分配给lineNumber?

And*_*ren 7

尝试使用select项目索引到每个项目,如这篇msdn文章中所述:http://msdn.microsoft.com/en-us/library/bb534869.aspx

在你的情况下像这样(未测试):

var mydata = fileContent.Split('$')
             .Select(x => x.Trim())
             .Where(con => !String.IsNullOrEmpty(con))
             .Select((con, index) => new
                             {
                                 dataID = con.Substring(0, con.IndexOf('#')),
                                 dataElms = con.Split('#').ToArray(),
                                 dataCon = con,
                                 lineNumber = index
                             });
Run Code Online (Sandbox Code Playgroud)