Select from a DataTable - CSharp LINQ

CSharp examples for LINQ:Select

Description

Select from a DataTable

Demo Code


using System;// ww  w .ja  va2 s.com
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Data;

class MainClass
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\nUsing a data table source");
            // create the source
            DataTable table = createDataTable();

            // perform the query
            IEnumerable<string> dtEnum = from e in table.AsEnumerable() select e.Field<string>(0);
            // write out the elements
            foreach (string str in dtEnum)
            {
                Console.WriteLine("Element {0}", str);
            }
        }


        static DataTable createDataTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("name", typeof(string));
            string[] fruit = { "Oracle", "MySQL", "C", "fig", "XML", "file", "PLSQL" };
            foreach (string name in fruit)
            {
                table.Rows.Add(name);
            }
            return table;
        }
    }

Result


Related Tutorials