Practical Usage of Generic List as a Data Source in LINQ


Practical usage of Generic List<T> as a data source in LINQ.


Step 1 : Used Namespaces:

using System;
using System.Collections.Generic;
using System.Linq;

Step 2 : Used List<T> collection for Demo:

 List<string> list=new List<string>();
       list.Add("Arun");
       list.Add(
"Bala");
       list.Add("Parthiban"); 


Step 3 :
 Code Snippet: 
 
using
 System;
using System.Collections.Generic;
using System.Linq; 
namespace SampleTest
{
     public partial class LINQSample : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)

        {

            ListDataSource();
        }

         //Practical usage of Generic List<T> as a data source in Linq
        private void ListDataSource()

        {

            List<string> list = new List<string>();

            list.Add("Arun");
            list.Add("Bala");
            list.Add("Parthiban");

             //Retrive the list collection in query.

            var queryAllCustomers = from name in list

                                    select name;

            //Convert the Collection to List<T>.

            List<string> outPut = queryAllCustomers.ToList();

             //Display results

            foreach (string str in outPut)

            {

                 Response.Write(str);

            }

        }

    }

}

Step 4 :  Output:

img1.gif

Up Next
    Ebook Download
    View all
    Learn
    View all