1

I have a table called "PublicUserOfferingSignUp" which contains the following columns.

  • Id
  • PublicUserId - foreign key to PublicUser.Id
  • OfferingId
  • Created

My application is using Entity Framework, but I am getting stuck with how to join from the PublicUserOfferingSignUp table to the PublicUser table.

I want to obtain a list of PublicUserOfferingSignUp records but ordered by the Name column of the PublicUser table.

Currently I have this ....

return DataBase.PublicUserOfferingSignUps.Join(PublicUser, 

But I can't seem to work it out, any ideas ....

Steven

Can anybody help.

swade1987
  • 1,663
  • 5
  • 21
  • 28
  • possible duplicate of [entity framework join](http://stackoverflow.com/questions/3044153/entity-framework-join) –  May 09 '12 at 11:42

2 Answers2

4

Something like that

DataBase.PublicUserOfferingSignUps.Join(Database.PublicUsers, 
    puosu => puosu.PublicUserId,//PublicUserOfferingSignUps key
    pu => pu.Id,//PublicUser key
    (puosu, pu) => new {
        publicUsersOfferingSignUp = puosu,//we take all data from PubliUserOfferingSignUps
        puName = pu.Name//and Name from PublicUser
        })
.OrderBy(x => x.puName)//we order by PublicUser Name
.Select(x => x.publicUsersOfferingSignUp );//we take only the PublicOfferingSignUps

Edit : as @M.Schenkel noticed, it would be easier to have a

public virtual PublicUser PublicUser {get;set;}

in your PublicUserOfferingSignUp model

then the query would be

DataBase.PublicUserOfferingSignUps
.OrderBy(puosu => puosu.PublicUser.Name);

easier, no ?

Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
0

When you use the Entity Framework, the public user should be a property of your PublicUserOfferingSignUp-entity. If not, you can write a LINQ query to join them. For example:

var result = from pu in context.PublicUserOfferingSignUp
join u in context.PublicUser on u.id equals pu.PublicUserId 
select pu;

(this code is untested, but should give you the idea).

M.Schenkel
  • 477
  • 1
  • 3
  • 9