Read tab delimited with LINQ

DanielJay
DanielJay used Ask the Experts™
on
Hello - I am looking to import a Tab Delimited file into a dataset using LINQ and VB, the file is similar to this:

Account   Name        Address                   Town
------------------------------------------------------------------------------
123456      Mr Smith      123 Street      Some Town
789456      Mr Jones      456 Road                      Some Other Town

If anyone can help me out, I have looked at LINQ2CSV library but I want something basic in VB which I can build upon as I learn more about LINQ. Any help is appreciated.

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I have some code in C# for you, It has been a good while since I last used VB, and have in fact never used LINQ with it. It shouldn't be too hard to understand (I Hope). You can use the code by making the following c# call...

var tc = new TestClass("MyTabDelemitedFile");

var q = from c in tc
            where c["MyColName"] = "SomeVal"
            select c;

foreach (var c in q)
          Console.WriteLine(c["SomeOtherCol"]);
public class TestClass : IEnumerable<Dictionary<string, string>>
    {
        #region Properties
 
        /// <summary>
        /// Gets or sets the list of column names.
        /// </summary>
        public List<string> ColumnNames { get; set; }
 
        /// <summary>
        /// Gets or sets the file line data.
        /// </summary>
        public string[] Lines { get; set; }
 
        #endregion
 
        #region Constructor
 
        /// <summary>
        /// Creates a new instance of the class.
        /// </summary>
        /// <param name="fileName">The file to load.</param>
        public TestClass(string fileName)
        {
            var lines = File.ReadAllLines(fileName);
 
            //Copy all the data except tyhe heading and seperator lines.
            Lines = new string[lines.Length - 2];
            Array.Copy(lines, 2, Lines, 0, lines.Length - 2);
 
            //Line 1 is expected to have column names.
            var colNames = lines[0].Split(Convert.ToChar((Byte)9));
 
            //Create the column names list.
            ColumnNames = new List<string>(colNames);
        }
 
        #endregion
 
        #region IEnumerable<Dictionary<string,string>> Members
 
        public IEnumerator<Dictionary<string, string>> GetEnumerator()
        {
            foreach (var line in Lines)
            {
                //Split the line by tab
                var vals = line.Split(Convert.ToChar((Byte)9));
 
                var yld = new Dictionary<string, string>();
 
                for (int i = 0; i < ColumnNames.Count; i++)
                {
                    var val = string.Empty;
                    if (i < vals.Length)
                        val = vals[i];
 
                    yld.Add(ColumnNames[i], val);
                }
 
                //Yield the return result...
                yield return yld;
            }
        }
 
        #endregion
 
        #region IEnumerable Members
 
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
 
        #endregion
    }

Open in new window

Did this help you out?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial