score:25
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.
score: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
}
score: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.
score: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
Source: stackoverflow.com
Related Query
- Steps for a beginner to run very basic linq to sql query using Linqpad
- Dynamic linq query expression tree for sql IN clause using Entity framework
- Determine the source DataContext for a Linq to Sql query
- Using LINQ query result for data source for GridControl c#
- Query help for voting app in Linq using SQL Server
- LINQ Intersect Query for Many to Many relationship using .NET4 MVC4 Code First
- Using linq to select possible values for multiple database columns in one SQL query (EF6)
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- LINQ to SQL query using "NOT IN"
- SQL Query to LINQ syntax using not exist and join
- Dynamic query using LINQ to SQL
- How to reuse a linq expression for 'Where' when using multiple source tables
- Obtain max length for a "string" column using LINQ to SQL
- Can not run basic LINQ query
- Using LINQ vs SQL for Filtering Collection
- Create Linq Expression for Sql Equivalent "column is null" in c# by creating linq query dynamically
- linq - how do you do a query for items in one query source that are not in another one?
- How can I write the following code more elegantly using LINQ query syntax?
- Using ToList() on Enumerable LINQ query results for large data sets - Efficiency Issue?
- Unable to convert SQL Query to LINQ Query for Left Outer Join
- What are the prerequisites for using SQL and LINQ with my winforms application in C#?
- LINQ to SQL generates a different query for similar group by expressions
- Timeout Exception in SQL Server using via LINQ Query
- Using linq to query SQL Server is causing 100s of small queries
- Is possible to run a query with linq to search for a period of time?
- why the sql query is different on that linq query when run on c# and on vb.net?
- How to get SQL query into LINQ form in C# code
- EXTREMELY Poor LINQ Query Performance When Using Skip/Take for Paging
- How to work around the fact that Linq to SQL is using separate SQL DELETE statements for each item in DeleteAllOnSubmit
- Same SQL query when run with sp_executesql is fast, very slow if executed as dynamic query in query analyser, why?
More Query from same tag
- Hide some object fields before outputing?
- Nested Query or Sub query linq
- Cast exception of nested class in Linq query
- Linq Group by Month, Aggregate count
- Select only defined column in LINQ
- Declaring @model List<className> in a View and sending LINQ query result to it causes an error
- How two compare two array list items in linq query vb.net
- MVC Linq query with both group by columns and Max table column
- Order datagridview descending with linq
- Fill List<ViewModel> with strings from Regex.Matches
- The LINQ expression node type 'Lambda' is not supported in LINQ to Entities
- Getting a list of unique domains from email list
- Passing FieldNames and Values dynamically to linq
- C# LINQ NET 3.5 SP1: Using LINQ to group by two fields and assigning a correlative unique ID (integer number) to all member of the group
- List.OrderByDescending results in duplicates
- Get results from more than one table with a stored MySQL procedure in a controller in MVC entity framework 6
- OrderByDescending() per MSDN, what on earth does this mean?
- How to use .Where in generic list
- EF7 - How to get values from DbSet before change
- Linq query with list of flags
- WPF Linq Results to datagrid
- How to change value of child element based on attribute of child and parent?
- How to map extra additional property on Entity database call
- Linq to xml in c#
- In LINQ, select all values of property X where X != null
- Is there a difference between these two statements in Linq?
- ReSharper/Linq error: Access to modified closure
- How to typecast an imported class from an sql table
- Linq XML Dynamic building
- Parse XML into collection of Payments containing Invoices using LINQ