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.

1
  • possible duplicate of entity framework join Commented May 9, 2012 at 11:42

2 Answers 2

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 ?

Sign up to request clarification or add additional context in comments.

2 Comments

so what does this return? I want to return a list of PublicUserOfferingSignUp records
Yes, your question was rather clear... try it and if I missed something, I'll be glad to correct ;)
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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.