LINQ 了解非等值联接
我使用 asp.net 4、ef 4 和 c#、LINQ 和非等值连接。
下面我写了两个非等值连接的例子。 两者在我的模型中都运行良好。
因为我对 Linq 还很陌生,所以我想问您:
- 您建议我在代码中采用哪种语法类型?
- 哪个代码性能更快?
感谢您的帮助:
这里有一些有用的链接:
http://msdn.microsoft.com /en-us/library/bb882533.aspx
http://msdn .microsoft.com/en-us/library/bb311040.aspx
http ://msdn.microsoft.com/en-us/library/bb310804.aspx
// Query sintax
var queryContents =
from cnt in context.CmsContents
let cntA =
from a in context.CmsContentsAssignedToes
select a.CmsContent.ContentId
where cntA.Contains(cnt.ContentId) == false
select cnt;
// Query method
var queryContents02 =
from c in context.CmsContents
where !(
from a in context.CmsContentsAssignedToes
select a.ContentId).Contains(c.ContentId)
select c;
I use asp.net 4, ef 4 and c#, LINQ and Non-Equijoins.
Here below I wrote two examples of Non-Equijoins.
Both are working fine in my model.
Because I'm pretty new to Linq, I would like ask you:
- Which syntax typology would you advice me to adopt in my code?
- Which code performance faster?
Thanks for your help:
Here some useful links:
http://msdn.microsoft.com/en-us/library/bb882533.aspx
http://msdn.microsoft.com/en-us/library/bb311040.aspx
http://msdn.microsoft.com/en-us/library/bb310804.aspx
// Query sintax
var queryContents =
from cnt in context.CmsContents
let cntA =
from a in context.CmsContentsAssignedToes
select a.CmsContent.ContentId
where cntA.Contains(cnt.ContentId) == false
select cnt;
// Query method
var queryContents02 =
from c in context.CmsContents
where !(
from a in context.CmsContentsAssignedToes
select a.ContentId).Contains(c.ContentId)
select c;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会提示第三个选项:
或者(等效地):
我不希望性能受到影响 - 我希望所有这些最终都会得到相同的 SQL。
I'd prompt for a third option:
Or alternatively (and equivalently):
I wouldn't expect the performance to be impacted - I'd expect all of these to end up with the same SQL.
我喜欢第一个查询语法(它对我来说可读性更好,但这部分问题是主观的)并且我认为性能将是相同的,因为查询实际上是相同的。
let
关键字只是将子表达式存储到变量,但生成的 SQL 查询应该是“相同的”。I like the first query syntax (it is better readable for me but this part of question is subjective) and I think the perforamance will be the same because queries are actually the same.
let
keyword just stores subexpression to variable but generated SQL query should be the "same".