3
 var devSum = repository.Devices
                    .Where(dev => dev.Id == deviceId)
                    .Join(repository.ManagementGroups, device => device.ManagementGroupId, mGroup => mGroup.Id, (device, mGroup) => new { device, mGroup.Name })
                    .Join(repository.DataGroups, device_mGroup => device_mGroup.device.DataGroupId, dGroup => dGroup.Id, (device_mGroup, dGroup) => new { device_mGroup.device, managerName = device_mGroup.Name, dataName = dGroup.Name })
                    .Join(repository.DeviceTypes, d => d.device.TypeId, t => t.Id, (d, t) => new { d.device, d.dataName, d.managerName, TypeName = t.Name })
                    .SingleOrDefault();

Hi, I have the above query joining several tables and all was working. Then I realised some of the foreign keys may be empty.

I've researched the use of GroupBy and DefaultIfEmpty, and they sounded promising so I tried changing the first Join to GroupJoin on but this threw an error:

 var devSum = repository.Devices
                    .Where(dev => dev.Id == deviceId)
                    .GroupJoin(repository.ManagementGroups, device => device.ManagementGroupId, mGroup => mGroup.Id, (device, mGroup) => new { device, mGroup.Name })
                    .Join(repository.DataGroups, device_mGroup => device_mGroup.device.DataGroupId, dGroup => dGroup.Id, (device_mGroup, dGroup) => new { device_mGroup.device, managerName = device_mGroup.Name, dataName = dGroup.Name })
                    .Join(repository.DeviceTypes, d => d.device.TypeId, t => t.Id, (d, t) => new { d.device, d.dataName, d.managerName, TypeName = t.Name })
                    .SingleOrDefault();

'AnonymousType#1' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'AnonymousType#1' could be found (are you missing a using directive or an assembly reference?)

Can anyone help please?

DavidB
  • 2,566
  • 3
  • 33
  • 63

1 Answers1

3

mGroup in the line (device, mGroup) => new { device, mGroup.Name } actually represents the whole collection. You'd need to do a Select on it:

(device, mGroup) => new { Device = device, NameGroup = mGroup.Select(m => m.Name) }

Check out the MSDN page on GroupJoin for more info.

Corey Adler
  • 15,897
  • 18
  • 66
  • 80
  • 1
    thankyou IronMan, but that throws the error Error 17 Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access. – DavidB Mar 04 '13 at 15:59
  • 1
    Looks like the MSDN has that in their example. I've edited my answer accordingly. – Corey Adler Mar 04 '13 at 16:00
  • 1
    thanks, Thats got rid of the errors on that line at least! Ill keep fiddling to try and correct the rest myself and come back and accept this answer when i do. – DavidB Mar 04 '13 at 16:03
  • 1
    The other errors were just chnagign the names on the second line as they had changed on the first. Thanks for your help pal. – DavidB Mar 04 '13 at 16:09
  • No problem. Good luck to you on your project. – Corey Adler Mar 04 '13 at 16:14