How to select multiple columns in Linq- code

How to select multiple columns in Linq- code Linq select multiple columns.

You want to select multiple columns using Linq query. Here are and 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

Equivalent SQL query.

SELECT [t0].[ProductName] AS [NameOfProduct], [t0].[UnitPrice] AS [Price], [t0].[CategoryID] AS [Category]
FROM [Products] AS [t0] 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 _

End Sub

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

Extension method to select multiple columns.

   .Select (
      productList => 
            NameOfProduct = productList.ProductName, 
            Price = productList.UnitPrice, 
            Category = productList.CategoryID

Linq select all columns query.

void Main()
    var products = from productList in  Products
                   select productList;

Extension method select all columns.

   .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]


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


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 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.