views:

60

answers:

1

Hi,

I have SQL 2008 server with tables Employee, Phone and Email where Employee is in a one-to-many relation with Phone and Email (So employee can have many phone numbers and emails).

Now I want to query employee with all phones and emails:

var query = (from e in db.Employee
             select new EmployeeDTO
             {
                 Id = e.Id,
                 Name = e.Name,
                 Phones = e.Phones,
                 Emails = e.Emails
             }).ToList();

But linq translates it to very ugly sql, it makes left join with phone table but then it queries each employee emails in separate query like:

SELECT [t0].[EmployeeID], [t0].[Name], [t1].[Phone] (
    SELECT COUNT(*)
    FROM [dbo].[Phone] AS [t2]
    WHERE [t2].[EmployeeID] = [t0].[EmployeeID]
    ) AS [value]
FROM [dbo].[Employee] AS [t0]
LEFT OUTER JOIN [dbo].[Phone] AS [t1] ON [t1].[EmployeeID] = [t0].[EmployeeID]
ORDER BY [t0].[EmployeeID], [t1].[PhoneID]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.4926

SELECT [t0].[EmailID], [t0].[Email]
FROM [dbo].[Email] AS [t0]
WHERE [t0].[EmployeeID] = @x1
-- @x1: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [1ed2e9a0-ca4b-4912-8f8e-000ed8d0b2af]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.4926

SELECT [t0].[EmailID], [t0].[Email]
FROM [dbo].[Email] AS [t0]
WHERE [t0].[EmployeeID]= @x1
-- @x1: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [2203192b-05ef-4b59-b4b4-0010e26a8119]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.4926

What I need is simple sql:

SELECT [t0].[EmployeeID], [t0].[Name], [t1].[Phone], [t2].[Email]
FROM [dbo].[Employee] AS [t0]
LEFT OUTER JOIN [dbo].[Phone] AS [t1] ON [t1].[EmployeeID] = [t0].[EmployeeID]
LEFT OUTER JOIN [dbo].[Email] AS [t2] ON [t2].[EmployeeID] = [t0].[EmployeeID]

How can I write such linq query without grouping the results? I know how to make multiple left join, but then I need to group the results by EmployeeID.

A: 

Have you tried doing it like this?

var query = (from e in db.Employee
    join p in db.Phones on e.EmployeeID equals p.EmployeeID into tmpPhones
    from phones in tmpPhones.DefaultIfEmpty()
    join m in db.Emails on e.EmployeeID equals m.EmployeeID into tmpEmails
    from emails in tmpEmails.DefaultIfEmpty()

             select new EmployeeDTO
             {
                 Id = e.Id,
                 Name = e.Name,
                 Phones = phones,
                 Emails = emails
             }).ToList();

What you need is LoadWith to work with multiple 1 to many relationships, but that doesn't work -

http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/a68744f6-e1f3-4d28-82eb-87b8187ec0a8/

Jumping through hoops should work -

 var query = (from e in db.Employee
        join p in db.Phones on e.EmployeeID equals p.EmployeeID into tmpPhones
        from phones in tmpPhones.DefaultIfEmpty()
        join m in db.Emails on e.EmployeeID equals m.EmployeeID into tmpEmails
        from emails in tmpEmails.DefaultIfEmpty()
        group e by e into eGrp

                 select new EmployeeDTO
                 {
                     Id = eGrp.Key.Id,
                     Name = eGrp.Key.Name,
                     Phones = eGrp.Select(x => x.Phones),
                     Emails = eGrp.Select(x => x.Emails)
                 }).ToList();
Frank Tzanabetis
Yes, I have tried this, and this is so far the best solution BUT here I need to group results by EmployeeId to get distincts employees, because this query returns not distinct records!If you have 1 employee with 2 phones and 2 emails you get 4 records in results and need to group them.If you query only phones then Linq automagically groups it for you and you get 1 employee with 2 phones.But thanks for reply.
jojojohn