Table of Contents
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 syntax. To 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);
Related Topics
How to compose email and save email draft in outlook using C#
How to compose email and save email draft in outlook using C# […]
How to convert base64 string format of excel file into DataTable form of data type using C#
In this article, We learn how to convert base64 string format of excel file into Data Table form of data type using C#. I will […]
How to convert data from Aspose.Cells.Worksheet to DataTable using C#
How to conversion of data from Aspose.Cells.Worksheet to DataTable using C# […]
How to use Stored Procedure using Entity Framework in .Net Core
In this article, We can learn how to consume Stored Procedure (SP) using Entity Framework in .Net Core. I will show you how to do […]
Angular CRUD with .NET Core API
In this article, We create a full tutorial of Angular CRUD with .NET Core API. I will show how to do it, and just follow […]
Highly energetic article, I liked that bit. Will there be a part 2?