Reputation: 257
I have a query below. although can anyone point out what "from p" means? and also "var r"?
DataClasses1DataContext db = new DataClasses1DataContext();
var r = from p in db.Products
where p.UnitPrice > 15 // If unit price is greater than 15...
select p; // select entries
Upvotes: 2
Views: 174
Reputation: 1062510
r
is the composed query - an IQueryable<Product>
or similar; note the query has not yet executed - it is just a pending query. var
means "compiler, figure out the type of r from the expression on the right". You could have stated it explicitly in this case, but not all. But it wouldn't add any value, so var
is fine.
p
is a convenience marker for each product; the query is "for each product (p), restricting to those with unit price greater than 15 (where p > 15
), select that product (select p
) as a result.
Ultimately this compiles as:
IQueryable<Product> r =
db.Products.Where(p => p.UnitPrice > 15);
(in this case, a final .Select(p => p)
is omitted by the compiler, but with a non-trivial projection, or a trivial query, the .Select(...)
is retained)
Upvotes: 3
Reputation: 30097
from p
means any record from db.Product
and var
r means the collection of p
overall whole statements means give me all those records(p) from db.Products
where p.UnitPrice
is greater than 15
see this question to know more about var
Upvotes: 0
Reputation: 498904
The p
means each specific item in the collection referenced (db.Products
). See from
on MSDN.
var
is syntactic sugar - it resolves to the type returned from the LINQ query, assigning the type to the variable r
. See var
on MSDN.
For better understanding of LINQ, I suggest reading through 101 LINQ Samples.
Upvotes: 1