LinQ Tutorial

– This LINQ tutorial will show you some basic codes on implementing LINQ in C#.LINQ is an acronym for Language Integrated Query, which is descriptive for where it’s used and what it does. The Language Integrated part means that LINQ is part of programming language syntaxTo have an overall view of the definition of LINQ please refer to this site I find csharp-station.com. Below are basic codes for implementing LINQ on a DataTable.

For more tutorial visit this link ASP.NET MVC Developement.

CopyToDataTable();

Note:

  • Employee -> DataTable Name
DataTable ToDataTable = (from emp in Employee.AsEnumerable()
                         select emp).CopyToDataTable();
Select  All


Note:

  • Employee -> DataTable Name
var emp_selectAll = from emp in Employee.AsEnumerable()
select emp;

Select Specific column from DataTable

Note:

  • Name -> Table Column Name
  • Address -> Table Column Name
  • Employee -> DataTable Name
var emp_Select_column = from emp in Employee.AsEnumerable()
                               where emp.Field<string>("Name").Trim() == "name"
                               select new
                               {
                                   Name = emp.Field<string>("Name"), //variable
                                   LastName = emp.Field<string>("Address") //variable
                               };
Count()

  Note:

  • Name -> Table Column Name
  • Address -> Table Column Name
  • Employee -> DataTable Name
       var emp_Select_Count = (from emp in Employee.AsEnumerable()
                               where emp.Field<string>("Name").Trim() == "name"
                               select new
                               {
                                   Name = emp.Field<string>("Name"), //variable
                                   LastName = emp.Field<string>("Address") //variable
                               }).Count();
Count();

 Note:

  • Name -> Table Column Name
  • Employee -> DataTable Name
  • data -> Variable
 var Count = Employee.AsEnumerable().Count(data => data.Field<string>("Name") == "Jave");
ToList()

Note:

  • Name -> Table Column Name
  • Address -> Table Column Name
      var ToList = (from emp in Employee.AsEnumerable()
                               where emp.Field<string>("Name").Trim() == "name"
                               select new
                               {
                                   Name = emp.Field<string>("Name"),  //variable
                                   LastName = emp.Field<string>("Address") //variable
 
                               }).ToList();
SUM()

Note:

  • NUmberOfEmployee-> Table Column Name
  • data -> variable
  • Employee -> DataTable Name
    var Sum = Employee.AsEnumerable().Sum(data => data.Field<int>("NUmberOfEmployee"));
Group BY & order by
var data = dt.AsEnumerable() 
                                   .Where(a => DateTime.Parse(a.Field<string>("date")).Month == DateTime.Parse("01/01/2018").Month) 
                                   .GroupBy(x => new { dcount = x.Field<string>("ID") }) 
                                   .Select(group => new 
                                   { 
                                       x = group.Key.dcount, 
                                       Count = group.Count() 
                                   }) 
                                   .OrderByDescending(o => o.x);
var data = dt.AsEnumerable() 
                                   .Where(a => DateTime.Parse(a.Field<string>("date")).Month == DateTime.Parse("01/01/2018").Month) 
                                   .GroupBy(x => new { dcount = x.Field<string>("ID") }) 
                                   .Select(group => new 
                                   { 
                                       x = group.Key.dcount, 
                                       Count = group.Count() 
                                   }) 
                                   .OrderByDescending(o => o.x);

One Reply to “LinQ Tutorial on DataTable – C#”

Leave a Reply

Your email address will not be published. Required fields are marked *