LINQ understading Non-Equijoins

筅森魡賤 提交于 2019-12-12 02:33:05

问题


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;

回答1:


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.




回答2:


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".



来源:https://stackoverflow.com/questions/5260346/linq-understading-non-equijoins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!