CSharp - Use the var Keyword When Confused

Introduction

Sometime it may be a little unclear what data type you have.

You know it is an IEnumerable of some type T, but what is T?

You can assign the query results to a variable whose type is specified with the var keyword.

Then to get the type of the current value of that variable so you know what type T is.

Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind");  
  
var orders = db.Customers  
  .Where(c => c.Country == "USA" && c.Region == "WA")  
  .SelectMany(c => c.Orders);  
  
Console.WriteLine(orders.GetType());  

Related Topic