Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

how to use multiple where condition in linq

Description:
Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio that extends powerful query capabilities to the language syntax of C# and Visual Basic. Visual Studio includes LINQ provider assemblies that enable the use of LINQ with .NET Framework collections, SQL Server databases, ADO.NET Datasets, and XML documents.
LINQ allow us to use multiple where clause in query like the following below query:
var query = from r in tempData.AsEnumerable()
            where r.Field<string>("UserName") != "abc"
            where r.Field<string>("UserName") != "xyz"
            select r;

how to find grouping data and counter in linq

Description:
Here i will explain group by count multiple columns in linq. It is easy and short ways to find group by data with counter in linq like the below simple example to do it:

var result = (from emp in db.Employees
              where emp.companyID.Equals(companyid)
              group emp by emp.departmentID into g
              select new
              {
                deptid = g.Key,
                countemp = g.Count()
              }).ToList();

In above query we get the employees of selected companyid and than group by department wise and select that column that you want to display in the list.