In this chapter, I am adding some easy programming example of LINQ that will help you understand LINQ Query quickly. Forget the theory and just try to lean LINQ directly by programming. Believe me, after few examples you will feel that you are very familiar with LINQ.
Example 1: Create IList and store Product Name and Price and then display value using LINQ Query.
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqExample
{
class ProductStore
{
public string productName { get; set; }
public int productPrice { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Creating List
IList<ProductStore> productList = new List<ProductStore>();
productList.Add(new ProductStore { productName = "Hard Disk", productPrice = 1280 });
productList.Add(new ProductStore { productName = "Monitor", productPrice = 3000 });
productList.Add(new ProductStore { productName = "SSD Disk", productPrice = 3500 });
productList.Add(new ProductStore { productName = "RAM", productPrice = 2450 });
productList.Add(new ProductStore { productName = "Processor", productPrice = 7680 });
productList.Add(new ProductStore { productName = "Bluetooth", productPrice = 540 });
productList.Add(new ProductStore { productName = "Keyboard & Mouse", productPrice = 1130 });
//LINQ Query Syntax
var result = from p in productList
select p;
//LINQ Method Syntax. Uncomment it to see the result.
//var result = productList.Select(p => p);
foreach(var list in result)
{
Console.WriteLine("Product Name: {0} | Product Price : {1}",list.productName, list.productPrice);
}
Console.ReadKey();
}
}
}
Output:
Product Name: Hard Disk | Product Price : 1280
Product Name: Monitor | Product Price : 3000
Product Name: SSD Disk | Product Price : 3500
Product Name: RAM | Product Price : 2450
Product Name: Processor | Product Price : 7680
Product Name: Bluetooth | Product Price : 540
Product Name: Keyboard & Mouse | Product Price : 1130
_
Example 2: Filter products based on price
//LINQ Query Syntax var result = from p in productList where p.productPrice > 2000 && p.productPrice < 4000 select p; //LINQ Method Syntax. Uncomment it to see the result. //var result = productList.Where(p => p.productPrice > 2000 && p.productPrice < 4000);
Output
Product Name: Monitor | Product Price : 3000
Product Name: SSD Disk | Product Price : 3500
Product Name: RAM | Product Price : 2450
_
Example 3: Print Result using LINQ Syntax
//LINQ Query Syntax var result = from p in productList where p.productPrice > 2000 && p.productPrice < 4000 select p; result.ToList().ForEach(p => Console.WriteLine("Product Name: {0} | Product Price: {1}", p.productName, p.productPrice));
Output
Product Name: Monitor | Product Price: 3000
Product Name: SSD Disk | Product Price: 3500
Product Name: RAM | Product Price: 2450
_
Example 4: Find Product in List
//LINQ Query Syntax var result = from p in productList where p.productName.Contains("Disk") select p; //LINQ Method Syntax. Uncomment it to see the result. //var result = productList.Where(p => p.productName.Contains("Disk")); result.ToList().ForEach(p => Console.WriteLine("Product Name: {0} | Product Price: {1}", p.productName, p.productPrice));
Output
Product Name: Hard Disk | Product Price: 1280
Product Name: SSD Disk | Product Price: 3500
_
Example 5: List Product in Ascending Order
//LINQ Query Syntax var result = from p in productList orderby p.productName ascending select p; //LINQ Method Syntax. Uncomment it to see the result. //var result = productList.OrderBy(p => p.productName); result.ToList().ForEach(p => Console.WriteLine("Product Name: {0} | Product Price: {1}", p.productName, p.productPrice));
Output
Product Name: Bluetooth | Product Price: 540 Product Name: Hard Disk | Product Price: 1280 Product Name: Keyboard & Mouse | Product Price: 1130 Product Name: Monitor | Product Price: 3000 Product Name: Processor | Product Price: 7680 Product Name: RAM | Product Price: 2450 Product Name: SSD Disk | Product Price: 3500