简体   繁体   中英

C# - Entity Framework - Join method

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.

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 ?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM