简体   繁体   中英

Select all columns from one table and 1 column from another

I have the following query in 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);

But now I have a new requirement, I need to add a column 'rank.PreferredOrder' from table 'rank' into the result, is there an easy way of doing this without making a massive 'select' statement, because there are around 20-30 fields in creditCard alone.

I dont have your model in front of me, so can't confirm this or not, but you can use an anonymous object like this:

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}

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