Reading file during LINQ query

Viewed 82

I have a simple class, and I want to have the results from: (which are correct so far)

            Console.WriteLine(f.temp1);
            Console.WriteLine(f.temp2);

in my Class Definitions temp1=Name; temp2=id

public class Definitions
{
    public string Name { get; set; }
    public string Id { get; set; }
}

class Program
{

    static void Main()
    {
        ReadDefinitions();
    }

    public static void ReadDefinitions()
    {
        var files = from name in Directory.EnumerateFiles(Settings.Folder)
                    from id in File.ReadLines(name).Skip(2).Take(1)

                    select new
                    {
                        temp1= Path.GetFileNameWithoutExtension(name),
                        temp2= id

                    };

        foreach (var f in files)
        {
            Console.WriteLine(f.temp1);
            Console.WriteLine(f.temp2);
        }

        foreach (var f in files)
        {
            Console.WriteLine(f.temp1);
            Console.WriteLine(f.temp2);
        }
    }
}

I know this is stupid with this temp stuff, but I could not manage to do it directly. :(

The goal is to:

  • Read the directory with many thousand files...
  • Put the name into Definitions.Name
  • Put line 3 of every file into Definitions.Id

So that I can access them anytime in my Program.

(I still need to trim the 3 left characters of line AND the 4 right characters of it,..but I'll probably manage that myself)

1 Answers
Related