从一个表中选择所有列,从另一表中选择1列

时间:2019-02-22 14:51:09

标签: c# entity-framework entity-framework-core

我在linq中有以下查询:

(from creditCard in DbSet
join rank in base.dataContext.ProductVerticalRanks on creditCard.ProductVerticalReferenceId equals rank.ProductVerticalReferenceId
 where rank.ClientId == clientId
 orderby rank.PreferredOrder
 select creditCard)
 .Include(creditCard => creditCard.ProductVerticalCompany)
 .Include(creditCard => creditCard.Labels);

但是现在我有了一个新的要求,我需要在表“ rank”中添加一个列“ rank.PreferredOrder”到结果中,是否有一种简单的方法而无需进行大量的“ select”语句,因为仅信用卡中大约有20-30个字段。

1 个答案:

答案 0 :(得分:2)

我前面没有您的模型,因此无法确认,但是您可以使用这样的匿名对象:

from creditCard in DbSet
join rank in base.dataContext.ProductVerticalRanks on 
    creditCard.ProductVerticalReferenceId equals rank.ProductVerticalReferenceId into g
where rank.ClientId == clientId
orderby rank.PreferredOrder
select new {Card = creditCard, Ranks = g}