---
title: "Getting Started with Linq Queries"  
description: "Hi everyone in this blog I’m explaining about linq query.  Introduction:Language-Integrated Query (LINQ) is a set of features introduced in Visual Stu"  
author: "Anonymous User"  
published: 2015-02-23  
updated: 2015-02-23  
canonical: https://www.mindstick.com/blog/782/getting-started-with-linq-queries  
category: "linq"  
tags: ["linq to sql", "linq to object"]  
reading_time: 5 minutes  

---

# Getting Started with Linq Queries

Hi everyone in this blog I’m explaining about [linq query](https://www.mindstick.com/forum/33908/how-to-select-and-retrieve-data-using-linq-query-in-entity-framework).\

##### Introduction:

Language-[Integrated](https://www.mindstick.com/forum/33532/handler-pagehandlerfactory-integrated-has-a-bad-module-managedpipelinehandler-in-its-module-list) Query (LINQ) is a set of [features introduced](https://www.mindstick.com/forum/160961/what-are-the-key-features-introduced-in-java-8) in [Visual Studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) 2008 that extends powerful query [capabilities](https://www.mindstick.com/interview/490/explain-the-concepts-and-capabilities-of-assembly-in-dot-net) to the language syntax of C# and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the [technology](https://www.mindstick.com/articles/13005/the-functional-aspects-of-laser-technology) can be extended to support potentially any kind of data store. Visual Studio includes LINQ provider assemblies that enable the use of LINQ with .NET Framework [collections](https://www.mindstick.com/articles/12458/what-are-collections-in-c-sharp), SQL [Server databases](https://www.mindstick.com/forum/158359/what-are-some-common-techniques-for-backing-up-and-restoring-sql-server-databases), ADO.NET Datasets, and XML documents.

##### Basic Query:

```
// To achieve  Select * From ProductMst  you need to write following linq Query  var Result1 = from p in db.ProductMst                 select p;// to achieve Select ProductID, ProductName, Price from ProductMst you need to write following linq Query    var Result2 = from p in db.ProductMst                 Select new {                     p.ProductID,                     p.ProductName,                     p.Price                 }; 
```

##### Where Clause Query:

```
// to achieve Select * From ProductMst Where ProductID = 1 you need to write following linq Query    var Result3 = from p in db.ProductMst                 Where p.ProductID == 1                 Select p; // to achieve Select * From ProductMst Where SupplierId =2 and Price > 10 you need to write following linq Query    var Result4 = from p in db.ProductMst                 Where p.SupplierID == 2 && p.Price > 10                 Select p;  // to achieve Select * From ProductMst Where SupplierId =2 Or SupplierId=5 you need to write following linq Query    var Result5 = from p in db.ProductMst                 Where p.SupplierID == 2 || p.SupplierID == 5                 Select p;
```

##### Order By Query:

```
//To achieve   Select * From ProductMst Order By ProductId you need to write following linq Query    var Result6 = from p in db.ProductMst                 orderby p.ProductID                 select p; // to achieve Select * From ProductMst Order By ProductId Desc you need to write following linq Query    var Result7 = from p in db.ProductMst                 orderby p.ProductID descending                 select p; // to achieve Select * From ProductMst Order By CategoryId, Price Desc you need to write following linq Query    var Result8 = from p in db.ProductMst                 orderby p.CategoryID, p.Price descending                 Select p; 
```

##### Top Query:

```
//To achieve Select Top 5 * From ProductMst you need to write following linq Query    var Result9 = (from p in db.ProductMst                 Select p).Take (5); //To achieve Select Top 1 * From ProductMst you need to write following linq Query    var Result10 = (from p in db.ProductMst                   Select p).Take (1);     or     var Result11 = (from p in db.ProductMst                   select p).First(); 
```

##### Distinct Query:

//To achieve [Select Distinct](https://www.mindstick.com/forum/34065/how-to-find-select-distinct-address-in-entity-framework) CategoryId From ProductMst you need to write

\

following linq Query

```
    var Result13 = (from p in db.ProductMst                   Select p.CategoryID).Distinct ();
```

##### Group By Query:

//To achieve Select CategoryId, Count(CategoryID) As FieldName From

\

ProductMst Group By CategoryId you need to write following linq Query

```
    var Result14 = from p in db.ProductMst                  Group p by p.CategoryID into g                  Select new {                      CategoryId = g.Key,                      FieldName = g.Count()                  }; 
```

//To achieve Select CategoryId, Avg(UnitPrice) As NewField From

\

ProductMst Group By CategoryId you need to write follofing linq Query

```
    var Result15 = from p in db.ProductMst                  group p by p.CategoryID into g                  Select new {                      CategoryId = g.Key,                      FieldName = g.Average(S => S.Price)                  };
```

##### Union Query:

//To achieve Select * From ProductMst Where CategoryId =1 union Select *

\

From ProductMst Where CategoryId = 2 you need to write follofing linq

\

Query

```
    var Result17 = (from p in db.ProductMst                   Where p.CategoryID == 1                   Select p).Union (                       From m in db.ProductMst                       Where m.CategoryID == 2                       Select m                   );
```

in my next post i'll explain about [Introduction of Troubleshooting](https://www.mindstick.com/blog/785/Introduction%20of%20Troubleshooting#.VQEw_vmUdz8)

---

Original Source: https://www.mindstick.com/blog/782/getting-started-with-linq-queries

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
