Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to LINQ. I have a folowing SQL query and I am trying to get LINQ equivalent statement.
SQL
SELECT orgNm.OrgNm
FROM OrgNm orgNM 
INNER JOIN ProspectClient pc
ON orgNM.OrgNmTypeId = 2
AND pc.ProspectClientID = orgNm.OrgId
AND orgNm.OrgId NOT IN (SELECT CloneOrgId FROM ClonedOrgInfo)
AND orgnm.orgId NOT IN (SELECT RelatedOrgId FROM OrgcRlshp)



Thanks...

AJ

[Edit]Code block added, 'SQL' tag added[/Edit]
Posted
Updated 8-Jan-13 6:27am
v2

Let's clarify, just because you add something in the JOIN ON clause, does not mean it belongs there. Your query should be this one:
SQL
SELECT orgNm.OrgNm
FROM OrgNm orgNM
INNER JOIN ProspectClient pc ON pc.ProspectClientID = orgNm.OrgId
WHERE orgNM.OrgNmTypeId = 2
AND orgNm.OrgId NOT IN (SELECT CloneOrgId FROM ClonedOrgInfo)
AND orgnm.orgId NOT IN (SELECT RelatedOrgId FROM OrgcRlshp)


Here is one possible interpretation in three steps:
C#
var re = from o in OrgNm
         join pc in ProspectClient on o.OrgId = pc.ProspectClientID
         where o.OrgNmTypeId = 2;

var ex = ClonedOrgInfo.Select(x => x.CloneOrgId).Union(OrgcRlshp.Select(y => y.RelatedOrgId));

var result = re.Where(r => !ex.Contains(re.OrgId));


Since LINQ to SQL is using deferred execution, it does not matter how many steps you use.
 
Share this answer
 
v2
Comments
Thomas Daniels 8-Jan-13 13:33pm    
Good answer, +5!
Zoltán Zörgő 8-Jan-13 13:36pm    
Thank you.
Thanks a ton, Zoltán Zörgő... Your solution solves my question!

As I am new to LINQ, could you suggest me a better approach/knowledge base where I can leverage my LINQ coding skills?


Regards
AJ
 
Share this answer
 
Comments
Zoltán Zörgő 8-Jan-13 15:24pm    
You are welcome, but you have added a comment as an answer to the question. You should have used the "Have a question or a comment?" button...
Anyway: have a look here: http://www.linqpad.net/ This is a great tool not only to learn, but also for productive tasks. And it has several built-in samples. Just play with it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900