views:

101

answers:

1

I have the following SQL query:-

select distinct * from dbo.Profiles profiles 
left join ProfileSettings pSet on pSet.ProfileKey = profiles.ProfileKey 
left join PlatformIdentities pId on pId.ProfileKey = profiles.Profilekey

I need to convert it to a LinqToEntities expression. I have tried the following:-

from profiles in _dbContext.ProfileSet
                            let leftOuter = (from pSet in _dbContext.ProfileSettingSet
                                             select new
                                                        {
                                                            pSet.isInternal
                                                        }).FirstOrDefault()
 select new
                                       {
                                           profiles.ProfileKey,
                                           Internal = leftOuter.isInternal,
                                           profiles.FirstName,
                                           profiles.LastName,
                                           profiles.EmailAddress,
                                           profiles.DateCreated,
                                           profiles.LastLoggedIn,                                               
                                       };

The above query works fine because I haven't considered the third table "PlatformIdentities". Single left outer join works with what I have done above. How do I include PlatformIdentities (the 3rd table) ? I basically want to translate the SQL query I specified at the beginning of this post (which gives me exactly what I need) in to LinqToEntities.

Thanks

A: 

Let me know if you want to select something different, but a true join is below

  from p in _dbContext.ProfileSet
                join ps in _dbContext.ProfileSettings on p.ProfileKey = ps.ProfileKey  into a
                join pi in _dbContext.PlatformIdentities on p.ProfileKey = pi.ProfileKey  into b

                select new
                {
                   profiles.ProfileKey,
                   profiles.FirstName,
                   profiles.LastName,
                   profiles.EmailAddress,
                   profiles.DateCreated,
                   profiles.LastLoggedIn, 
                   PlatformSettings = a.Select(x=>x),
                   PlatformIdentities = b.Select(y=>y)
                }
Nix
The above query won't work, because the foreign key column (ProfileKey) cannot be accessed as a property in linq to entities like it can be in linq to sql. This will be fixed in EF v4.0 that is coming out with .NET 4.0 when it launches next month from what I have read.
You will get an error similar to this:-'DAL.EntityModels.ProfileSetting' does not contain a definition for 'ProfileKey' and no extension method 'ProfileKey' accepting a first argument of type 'DAL.EntityModels.ProfileSetting' could be found (add a using directive or assembly reference)
I based that join on the SQL you provided, it sounds like you have changed your conceptual model.
Nix