How to select multiple columns in Linq- C#.net VB.net code

How to select multiple columns in Linq- C#.net VB.net code Linq select multiple columns.

You want to select multiple columns using Linq query. Here are C#.net and VB.net examples. We will query Products table of North wind sample database and select three columns out of it.

  1. Linq Chapter 1 – Split string to list of strings.
  2. Linq Chapter 2 – Remove duplicates from list.
  3. Linq Chapter 3 – Linq Where Clause.
  4. Linq Chapter 4 – Linq Group by multiple clause.
  5. Linq Chapter 5 – Linq Inner Join query.
  6. Linq Chapter 6 – Linq Distinct list.
  7. Linq Chapter 7 – Linq OfType Method.
  8. Linq Chapter 8 – Select Multiple Columns in Linq query.

C# Linq select multiple columns to list.

void Main()
{
    
    var products = from productList in  Products
                   select new { NameOfProduct = productList.ProductName
                                 , Price = productList.UnitPrice  
                              , Category = productList.CategoryID
                              };
                   
                   products.Dump();
}

Equivalent SQL query.

SELECT [t0].[ProductName] AS [NameOfProduct], [t0].[UnitPrice] AS [Price], [t0].[CategoryID] AS [Category]
FROM [Products] AS [t0]

VB.net Linq to select multiple columns.

Private Sub Main()

	Dim products__1 = From productList In ProductsNew With { _
		Key .NameOfProduct = productList.ProductName, _
		Key .Price = productList.UnitPrice, _
		Key .Category = productList.CategoryID _
	}

	products__1.Dump()
End Sub

In select statement we are giving anonymous names to actual columns in table.

Extension method to select multiple columns.

Products
   .Select (
      productList => 
         new  
         {
            NameOfProduct = productList.ProductName, 
            Price = productList.UnitPrice, 
            Category = productList.CategoryID
         }
   )

Linq select all columns query.

void Main()
{
    
    var products = from productList in  Products
                   select productList;
                   
                   products.Dump();
}

Extension method select all columns.

Products
   .Select (productList => productList)

Equivalent Select query.

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
FROM [Products] AS [t0]

Tags:

Linq Group By Multiple ColumnsAsp.net delete file file on Server C#.net and VB.net codeListbox select multipleHow to get Jquery selected optionPaypal purpose code IndiaASP net Write Text On Image C#.net and Vb.net programOne dimensional array example C#.net VB.netGet Selected Value From Dropdownlist [JQuery]Read Text File in (C#.net or VB.net)Jquery Dropdown Selected Valuelistbox to listbox

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.