LINQ Underestimates Non-Equijoins

I am using asp.net 4, ef 4 and C #, LINQ and Non-Equijoins.

Below I wrote two examples of Non-Equijoins. Both work great in my model.

Since I'm pretty new to Linq, I would like to ask you:

  • What syntax typology would you recommend me to accept in my code?
  • What code performance is faster?

Thank you for your help:

Here are 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; 
0
source share
2 answers

I would suggest 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 would not expect performance to be affected - I expect that they will all end up with the same SQL.

+4
source

I like the first query syntax (it reads better for me, but this part of the question is subjective), and I think the performance will be the same because the requests are actually the same. let keyword simply stores the subexpression of the variable, but the generated SQL query should be "the same".

+1
source

Source: https://habr.com/ru/post/1343142/


All Articles