Linq to SQL 简单问题
我在下面有一个疑问。不过谁能指出“from p”是什么意思?还有“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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
r
是组合查询 -IQueryable
或类似查询;请注意,查询尚未执行 - 它只是待处理的查询。var
的意思是“编译器,从右边的表达式中找出 r 的类型”。在这种情况下,您可以明确说明这一点,但不是全部。但它不会增加任何价值,所以 var 就可以了。p
是每个产品的方便标记;查询是“对于每个产品 (p),仅限于单价大于 15 的产品(其中 p > 15
),选择该产品(选择 p
)为 最终编译结果为:(
在本例中,编译器省略了最终的.Select(p => p),但有一个不平凡投影或一个简单的查询,
.Select(...)
被保留)r
is the composed query - anIQueryable<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, sovar
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:
(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)p
表示引用的集合中的每个特定项目 (db.Products
)。请参阅 MSDN 上的来自
。var
是语法糖 - 它解析为从 LINQ 查询返回的类型,并将该类型分配给变量r
。请参阅 MSDN 上的var
。为了更好地理解 LINQ,我建议阅读101 LINQ 示例。
The
p
means each specific item in the collection referenced (db.Products
). Seefrom
on MSDN.var
is syntactic sugar - it resolves to the type returned from the LINQ query, assigning the type to the variabler
. Seevar
on MSDN.For better understanding of LINQ, I suggest reading through 101 LINQ Samples.
from p
表示来自db.Product
的任何记录,var
r 表示p 的集合
整个语句表示给出我来自
db.Products
的所有记录(p),其中p.UnitPrice
大于 15,请参阅 这个问题了解有关
var
的更多信息from p
means any record fromdb.Product
andvar
r means thecollection of p
overall whole statements means give me all those records(p) from
db.Products
wherep.UnitPrice
is greater than 15see this question to know more about
var