LINQ 了解非等值联接

发布于 2024-10-21 03:15:49 字数 1125 浏览 2 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∝单色的世界 2024-10-28 03:15:49

我会提示第三个选项:

var validContentIds = from a in context.CmsContentsAssignedToes 
                      select a.ContentId;

var queryContents = from cnt in context.CmsContents
                    where !validContentIds.Contains(cnt.ContentId)
                    select cnt;

或者(等效地):

var validIds = context.CmsContentsAssignedToes.Select(a => a.ContentId);

var queryContents = context.CmsContents
                           .Where(cnt => !validIds.Contains(cnt.ContentId));

我不希望性能受到影响 - 我希望所有这些最终都会得到相同的 SQL。

I'd prompt for a third option:

var validContentIds = from a in context.CmsContentsAssignedToes 
                      select a.ContentId;

var queryContents = from cnt in context.CmsContents
                    where !validContentIds.Contains(cnt.ContentId)
                    select cnt;

Or alternatively (and equivalently):

var validIds = context.CmsContentsAssignedToes.Select(a => a.ContentId);

var queryContents = context.CmsContents
                           .Where(cnt => !validIds.Contains(cnt.ContentId));

I wouldn't expect the performance to be impacted - I'd expect all of these to end up with the same SQL.

人生百味 2024-10-28 03:15:49

我喜欢第一个查询语法(它对我来说可读性更好,但这部分问题是主观的)并且我认为性能将是相同的,因为查询实际上是相同的。 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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文