Search code examples
c#linqjoinlinqpad

Simple Linqpad Linq join statement, select all fields


I'm trying to do a simple join query in Linqpad, to mimic the following ...

SELECT *     
FROM Companies C    
JOIN Addresses A    
ON A.CompanyID = C.CompanyID    
WHERE C.CompanyID = 123

I'm using C# Expression (so no need to Dump, AFAIK), and tried the following unsuccessfully ...

from C in Companies    
join A in Addresses on C.CompanyID equals A.CompanyID    
where C.CompanyID = 123    
select C,A

So the result is "name does not exist in current context"

I suspect it might be because of common field in both tables, or the C,A syntax is basically wrong.

I want to be able to do the splat "*" across both tables. Is this possible?


Solution

  • I believe your select should look like

    select new { C, A }
    

    or

    select new { Company = C, Address = A }
    

    as you are returning more than one complex object, so it should be wrapped with an anonymous type. Or you can define your type and then set the properties like so

    select new MyType { Company = C, Address = A }