Prot
Prot

Reputation: 147

How to write a DataTable to a Linq entity?

I have a DataTable filled with coordinate points. I also have a LINQ entity which also contains two columns for all datapoints.

Is it possible to somehow copy the DataTable-information to the LINQ entity, so I can write this table to my database?

Here I fill my table with Information reading it from a txt file:

public static DataTable readAscii(FileStream fileStream)
        {
            DataTable spectrumDT = new DataTable();
            DataTable InfoDT = new DataTable();
            StreamReader sr = new StreamReader(fileStream);
            string line;

            while ((line = sr.ReadLine()) != null)
            {
                string[] coords = line.Split('\t');

                if (spectrumDT.Columns.Count == 0)
                {
                    spectrumDT.Columns.Add(new DataColumn("X", typeof(string)));
                    spectrumDT.Columns.Add(new DataColumn("Y", typeof(string)));                    
                }

                if (coords.Length > 0 && !string.IsNullOrWhiteSpace(coords[0].ToString()))
                {

                    try
                    {
                        spectrumDT.Rows.Add(coords[0].ToString(), coords[1].ToString());
                    }
                    catch {
                        break;
                    }

                }

            }

            return spectrumDT;
        }

My LINQ-entity also has those X and Y values for columns.

thanks in advance

Upvotes: 1

Views: 324

Answers (1)

Hassan Boutougha
Hassan Boutougha

Reputation: 3919

 DataTable dt =  readAscii(yourfileStream);


 var query = from p in dt.AsEnumerable()
                        select new
                        {
                            x= p.Field<string>("X"),
                            y= p.Field<string>("Y")                         
                        };

Upvotes: 2

Related Questions