5

I am using a stream reader to read a text file and then using Linq for retrieving the information

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 = 
                    };

I would also like to get the line number. I tried using Index but I was not able to. How to query to get the index and assign it to lineNumber?

user425381
  • 53
  • 1
  • 4
  • Could you post some example input files? It could provide much more efficient code if we had a better idea of what it looks like. – Jeff Mercado Aug 11 '11 at 07:59

6 Answers6

8

Try using the select that projects index into each item, as given in this msdn article: http://msdn.microsoft.com/en-us/library/bb534869.aspx

In your case something like this (not tested):

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
                             });
Andreas Ågren
  • 3,879
  • 24
  • 33
1

For starters, I would not read the file in as a big string. Use methods that could process it in small chunks. Use File.ReadLines() for example to read through the file line by line. It will be easier to get line numbers this way and much more efficient than reading it all at once only to split it up again.

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., You should stick to the usual C# naming conventions. Public properties of your classes should use PascalCasing, not camelCasing and should not be abbreviated.

The code you use to process the content looks awkward. It could probably be improved if I knew what the files looked like. I'll leave that out until you could show us how it is.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
0

Try This:

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.
John Reese
  • 21
  • 7
0

How about this?

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

or in your case...

Index = fileContent.IndexOf(con)
sgtz
  • 8,849
  • 9
  • 51
  • 91
  • that won't work if there are two equal lines in filecontent.split('$') – hcb Aug 11 '11 at 07:47
  • in that case, number the lines first with a select at the start such as .Select(...).Split(the rest of your query). – sgtz Aug 11 '11 at 07:59
0

if this whole data is going in myData then you can use the index directly from the myData.

Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
-1

you can try something like this

long index = 0;
var xElementsAndNodes = from xmlElement in elementsColl
select new
{
  Index = index += 1,
  ....
}
display name
  • 4,165
  • 2
  • 27
  • 52
Victor
  • 1