Steps for a beginner to run very basic linq to sql query using Linqpad

Silverlight Student picture Silverlight Student · Nov 3, 2011 · Viewed 31.7k times · Source

Trying to learn Linq using LinqPad and getting frustated with how to start on it. Let's say I want to write a C# Expression and a C# statment where I have a table in SQL server named Products and I want to pull all rows where price is greater then 50. How would yo write it?

Answer

Joe Albahari picture Joe Albahari · Nov 4, 2011

Let's say I want to write a C# Expression and a C# statment where I have a table in SQL server named Products and I want to pull all rows where price is greater then 50. How would yo write it?

LINQPad builds the typed DataContext for you automatically, so you don't need to instantiate anything. In C# expression mode, just type the folowing:

Products.Where(p => p.Price > 50)

and press F5. Alternatively, you might prefer to use a query expression:

from p in Products
where p.Price > 50
select p

In C# statements mode, you'll need to call the Dump() method to tell it to write out the results. You'll also need to terminate the expression with a semicolon:

Products.Where(p => p.Price > 50).Dump();

There are more examples in the samples section of LINQPad - look at the 5-minute induction.