17

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?

Silverlight Student
  • 3,968
  • 10
  • 37
  • 53

4 Answers4

27

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.

Joe Albahari
  • 30,118
  • 7
  • 80
  • 91
  • Thanks @Joe. That's exactly what i was looking for. BTW exprssion worked fine but when I run "from p in Products where p.Price > 50 select p" statment I get error "; expected". I add ; at end but that does not help either. Any ideas? – Silverlight Student Nov 04 '11 at 13:48
  • that's because you've got C# Statement(s) selected. Statements must end with ; but expressions cannot end with ; I just skip all this confusion and go with C# Program :) – Joe Nov 29 '11 at 03:36
  • @Joe can you help me with this question :) http://stackoverflow.com/questions/19291255/linqpad-doesnt-render-group-collection – AuthorProxy Nov 03 '13 at 18:07
  • @alex-v-kostyukov, i think you meant to ask @ joe-albahari and not @ joe. it's a bit confusing because he answered the question but I commented on it and it appears we are the same person. Compounded by the fact that my username is joe :) I was here first! – Joe Nov 04 '13 at 03:33
11

Just wanted to add - LINQ pad pluralizes - I did not know this and it drove me crazy for a good fifteen minutes

I was trying to select from a table called DentalApplication

    DentalApplication.Where(a=> a.PackageID > 0)

Gave me this error

    'LINQPad.User.DentalApplication' does not contain a definition for 'Where'

Changed it to

    DentalApplications.Where(a=> a.PackageID > 0)

and it worked

ff0000e2
  • 417
  • 7
  • 15
  • For default, Linqpad pluralizing child association properties. Please refer to http://stackoverflow.com/questions/7833133/linqpad-adds-an-s-to-the-end-of-every-table – maoyang Dec 08 '15 at 22:38
3
var db = new MyDatabaseContext();  // Your database context.
var result = db.Products.Where(q=>q.Price > 50);

...where db represents your ORM context. Price represents your mapping to the Price field in the database. result represents the result set -- your database rows/entities.

George Johnston
  • 31,652
  • 27
  • 127
  • 172
  • It gives "The type or namespace name 'MyDatabaseContext' could not be found". Sorry for my ignorance but I am totally new to Linq/ORM. Do i need to setup something in Linqpad? My impression that I can just use linq to sql just like I use my SQL management studio and directly query database tables – Silverlight Student Nov 03 '11 at 18:39
  • You need to create a database mapping. `MyDatabaseContext` just is a dummy name for whatever you called your ORM. – George Johnston Nov 03 '11 at 19:01
  • Do you know where exactly can I create mapping in LinqPad? – Silverlight Student Nov 03 '11 at 19:30
  • Reading the link at http://blog.trekforth.com/2009/07/querying-sql-with-linqpad.html, does not seem like i need to create any mapping – Silverlight Student Nov 03 '11 at 20:41
0

Check out: http://msdn.microsoft.com/en-us/library/bb397933(v=vs.90).aspx which will give you an introduction to Linq and using lambda expressions.

Then have a look at http://msdn.microsoft.com/en-us/library/bb386927.aspx which will give you the basics, but to answer your specific question:

var products = db.Products.Where(prod => prod.Price > 50);
foreach(var product in products)
{
     //do something
}
Maess
  • 4,118
  • 20
  • 29
  • When I run it as a C# program, i get following error "'LINQPad.User.Products' does not contain a definition for 'Price' and no extension method 'Price' accepting a first argument of type 'LINQPad.User.Products' could be found". What am I doing wrong? – Silverlight Student Nov 03 '11 at 18:49
  • You need to create a database mapping. Did you have a look at the Linq 2 SQL tutorial link I posted? That covers the basics. Do you have a database set up with a products table? – Maess Nov 03 '11 at 19:09
  • Yeah I have seen that but In LinqPad the only option i seem to find is Add Connection. Once I added connection to my SQL Server db instance, i can see all databases/tables etc on left side. I just don't know or see any optons how and where to create database mapping – Silverlight Student Nov 03 '11 at 19:30
  • Sorry, I am not familiar with LinqPad. – Maess Nov 04 '11 at 12:53