CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2006
    Posts
    220

    LINQ question (JOINs related)

    Hi

    I am having trouble converting a query that contains AND operator in JOIN into LINQ. For example, our sample query is

    Code:
    SELECT
       e.name, a.city
    FROM
       Emp e INNER JOIN Address a ON e.id = a.emp_id AND e.country = a.country
    WHERE
       e.prof = 'doctor'
    The main thing that is confusing me how to deal with the last AND clause.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: LINQ question (JOINs related)

    Try this:
    Code:
    var query = 
       from e in Emp
       join a in Address on new {f1 = e.id, f2 = e.country} equals new {f1 = a.emp_id, f2 = a.country} into temp
       from t in temp.DefaultIfEmpty()
       where t.prof = "doctor"
       select new {t.name, t.city};
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Apr 2006
    Posts
    220

    Re: LINQ question (JOINs related)

    Quote Originally Posted by cilu
    Try this:
    Code:
    var query = 
       from e in Emp
       join a in Address on new {f1 = e.id, f2 = e.country} equals new {f1 = a.emp_id, f2 = a.country} into temp
       from t in temp.DefaultIfEmpty()
       where t.prof = "doctor"
       select new {t.name, t.city};
    I think that you converted it into LEFT OUTER JOIN. will try it shortly

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured