简体   繁体   English

C# - 实体框架 - Join方法

[英]C# - Entity Framework - Join method

I have a table called "PublicUserOfferingSignUp" which contains the following columns. 我有一个名为“PublicUserOfferingSignUp”的表,其中包含以下列。

  • Id ID
  • PublicUserId - foreign key to PublicUser.Id PublicUserId - PublicUser.Id的外键
  • OfferingId 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. 我的应用程序正在使用实体框架,但我不知道如何从PublicUserOfferingSignUp表加入到PublicUser表。

I want to obtain a list of PublicUserOfferingSignUp records but ordered by the Name column of the PublicUser table. 我想获取PublicUserOfferingSignUp记录的列表,但是按PublicUser表的Name列排序。

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 编辑:正如@ M.Schenkel注意到的那样,拥有一个更容易

public virtual PublicUser PublicUser {get;set;}

in your PublicUserOfferingSignUp model 在您的PublicUserOfferingSignUp模型中

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. 使用实体框架时,公共用户应该是PublicUserOfferingSignUp-entity的属性。 If not, you can write a LINQ query to join them. 如果没有,您可以编写LINQ查询来加入它们。 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). (此代码未经测试,但应该给你一个想法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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