3

I'm new in LINQ , i have query which i wrote with SQL server and i wanna Translate/convert to LINQ.Can anyone direct me in the right direction? thx

This is my SQL:

SELECT Contact.Name, Contact.E-Mail, 
       Contact Business Relation.No_
FROM   Contact Business Relation RIGHT OUTER JOIN
          Contact ON Contact Business Relation.Contact No_ = Contact.Company No_
WHERE  (Contact.E-Mail = @mail)


This is my LINQ (I'm not sure i did it in right way):

                var query = from cbr in db.Contact_Business_Relation
                           join c in db.Contact
                           on cbr.Contact_No_ equals c.Company_No_ into f
                           from c in f.DefaultIfEmpty()
                           where c.E_Mail == Mail
                           select new
                           {
                               Mail = c.E_Mail,
                               No = cbr.No_
                           };
Erik Philips
  • 53,428
  • 11
  • 128
  • 150

1 Answers1

1

Unfortunately there are no right joins in linq. However, you can rearrange your query to use a left join instead.

var query = from c in db.Contact
            join cbr in db.Contact_Business_Relation
            on c.Company_No_ equals cbr.Contact_No_ into f
            from cbr in f.DefaultIfEmpty()
            where c.E_Mail == Mail
            select new
            {
              Mail = c.E_Mail,
              No = cbr.No_
            };

Join Clause

Aducci
  • 26,101
  • 8
  • 63
  • 67
  • To bad :( , but i will try dat , tnx aducci –  Oct 12 '17 at 15:19
  • 2
    Better fix the original problem - using entities as if they were tables instead of configuring relations between the objects. All this code should be replaced with a single `db.Contact.Include(c=>c.Relations).Where(c=>c.E_Mail == Mail)` – Panagiotis Kanavos Oct 12 '17 at 15:31
  • @PanagiotisKanavos - agreed, but sometimes you need to use a manual join – Aducci Oct 12 '17 at 15:38