In the statement var v = prod.Select(a => a.productName); a is nothing but a placeholder or act as a parameter that represent object 'prod'.
private void SimpleSelectTwo()
{
List
{
new Product() { productID=1, productName="Cooking Oil", productPrice=21.5, stockQuantity=10 },
new Product() { productID=2, productName="Washing Powder", productPrice=38, stockQuantity=10 },
new Product() { productID=3, productName="Tea bags", productPrice=110, stockQuantity=100 },
new Product() { productID=4, productName="Garbase bags", productPrice=21.5, stockQuantity=12 },
new Product() { productID=5, productName="Health drink", productPrice=85, stockQuantity=18 },
new Product() { productID=6, productName="Cool drinks", productPrice=45, stockQuantity=15 }
};
Console.WriteLine("\nBefore LINQ :");
foreach (Product pr in prod)
{
Console.WriteLine("ProdId: {0}, ProdName: {1}, ProdPrice: {2}, Stock: {3} ", pr.productID, pr.productName,pr.productPrice, pr.stockQuantity );
}
var v = prod.Select(a => a.productName);
Console.WriteLine("\nAfter LINQ : only Product is selected");
foreach(string pr in v )
{
Console.WriteLine("Product Name : {0}", pr);
}
}
OUTPUT
Before LINQ :
ProdId: 1, ProdName: Cooking Oil, ProdPrice: 21.5, Stock: 10
ProdId: 2, ProdName: Washing Powder, ProdPrice: 38, Stock: 10
ProdId: 3, ProdName: Tea bags, ProdPrice: 110, Stock: 100
ProdId: 4, ProdName: Garbase bags, ProdPrice: 21.5, Stock: 12
ProdId: 5, ProdName: Health drink, ProdPrice: 85, Stock: 18
ProdId: 6, ProdName: Cool drinks, ProdPrice: 45, Stock: 15
After LINQ : only Product name is selected
Product Name : Cooking Oil
Product Name : Washing Powder
Product Name : Tea bags
Product Name : Garbase bags
Product Name : Health drink
Product Name : Cool drinks