Answered Using LINQ with flat text files

  • Wednesday, April 09, 2008 5:03 AM
     
     

    In the past I have found the "Microsoft Text Driver" a great answer to some programming jobs.

     

    (For those who haven't used it:  This is a driver that connects directly to flat files (comma separated or whatever) and allows them to be handled like a database, including SQL queries and writing data.  The relations etc. are defined in a file called "schema.ini".)

     

    I'm currently looking at a job where LINQ against flat files would be a very good approach.

     

    (There is only ever one user, if the data were imported into a dB it would likely be dumped again immediately...)

     

    My search has not shown anybody doing this sort of thing with LINQ.

     

    Is there an existing way to do this?

All Replies

  • Friday, April 11, 2008 7:42 AM
     
     Answered

    Hi MikeGale,

    As far as I know, LINQ can query some .Net containers and collections, such as list generic class, please check the code snippet below.

    Code Snippet

    var aList = new List<FieldContainer>

                {

                    new FieldContainer("a1"),

                    new FieldContainer("a2"),

                    new FieldContainer("a3"),

                    new FieldContainer("a4"),

                    new FieldContainer("a5")

                };

      var r1 = from a in aList

                         where a.Text=="a1"

                         select a;

     

                FieldContainer value1 = null;

                foreach (var q1 in r1)

                    value1 = q1;

     

    Code Snippet

    public class FieldContainer

        {

            private string text;

            public string Text

            {

                get

                {

                    return this.text;

                }

                set

                {

                    this.text = value;

                }

            }

     

            public FieldContainer(string text)

            {

                Text = text;

            }

    }

     

     

    And you put the plain text to the list which wrapped by a custom class type.

    Regards,

    Xun

     

  • Friday, May 09, 2008 5:35 AM
     
     

    No that's not what I'm thinking about.

     

    I'd like to link up to a set of files, described in a scema.ini and treated as if they were a relational database.

     

    It was available in past MS database technologies and extremely useful (where it fits).

     

  • Friday, May 09, 2008 5:52 PM
     
     Answered

    This might do the trick:

     

    http://www.codeproject.com/KB/linq/LINQtoCSV.aspx

     

    Anders

     

  • Monday, May 12, 2008 11:34 PM
     
     

    Thanks Anders.

     

    I have just completed some tests of LINQToCSV.

     

    It does indeed do the job.

     

    The project was done using SQL Server, so I took some parts of it, made them into csv's, wrote a test app. to exercise them (C#) including joins and some simple LINQ queries.  When that worked I converted it into VB 9 and ran it there.  (There are a few differences but it all worked.  GREAT.)

     

    (I see the article was published 2 days after I asked the question.  Serendipity?)

     

    I'm impressed that you have the time to check these fora.  Thanks.