【问题标题】:line number while querying with linq使用 linq 查询时的行号
【发布时间】:2011-10-24 17:44:30
【问题描述】:

我正在使用流阅读器读取文本文件,然后使用 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 = 
                    };

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

【问题讨论】:

  • 您能发布一些示例输入文件吗?如果我们对它的外观有更好的了解,它可以提供更高效的代码。

标签: c# asp.net linq .net-4.0


【解决方案1】:

这个怎么样?

var animalList = from a in animals
                         select new { Animal = a, Index = animals.IndexOf(a) };

或者在你的情况下......

Index = fileContent.IndexOf(con)

【讨论】:

  • 如果 filecontent.split('$') 中有两条相等的行将不起作用
  • 在这种情况下,首先使用选择开头的行编号,例如 .Select(...).Split(查询的其余部分)。
【解决方案2】:

尝试使用将项目索引到每个项目中的选择,如这篇 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
                             });

【讨论】:

    【解决方案3】:

    如果整个数据都在 myData 中,那么您可以直接使用 myData 中的索引。

    【讨论】:

      【解决方案4】:

      对于初学者,我不会将文件作为大字符串读取。使用可以小块处理它的方法。例如,使用File.ReadLines() 逐行读取文件。以这种方式获取行号会比一次读取所有内容然后再次拆分更容易且效率更高。

      const string filePath = ...;
      var myData =
          from pair in File.ReadLines(filePath)
                           .Select((LineNumber, Line) => new { LineNumber, Line })
          where ...
          select new BaseSegment
          {
              ...
              Line = pair.Line,
              LineNumber = pair.LineNumber,
          };
      

      p.s.,您应该坚持通常的 C# 命名约定。您的类的公共属性应使用PascalCasing,而不是camelCasing,并且不应缩写。

      用于处理内容的代码看起来很别扭。如果我知道文件的样子,它可能会得到改进。在你能告诉我们它是怎么回事之前,我会忽略它。

      【讨论】:

        【解决方案5】:

        你可以试试这样的

        long index = 0;
        var xElementsAndNodes = from xmlElement in elementsColl
        select new
        {
          Index = index += 1,
          ....
        }
        

        【讨论】:

          【解决方案6】:

          试试这个:

          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 = Array.IndexOf(fileContent.Split('$').Select(x => x.Trim(),con)
                              };
          
          Array.IndexOf(yourArrey,the string you looking for); -> will return the index in the arrey.
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-11-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-08-30
            • 1970-01-01
            相关资源
            最近更新 更多