2017-01-22 294 views
0

添加專欄中,我產生的人與下面的查詢從母公司

var officers = db.RootDomains.Where(rd => rd.ID == id) 
            .SelectMany(rd => rd.Companies) 
            .SelectMany(c => c.CompanyMatches) 
            .Select(cm => cm.CompaniesHouseRecord) 
            .SelectMany(chr => chr.CompanyOfficers); 

這將返回我想要的人的名單,但我想包括在CompanyOfficers一個額外的列。

CompanyMatches實體有財產MatchMethod。我希望將此值附加到返回數據集中的每個CompanyOfficer上。

這可能嗎?

+0

你嘗試過動態對象.SelectMany(chr => chr.CompanyOfficers); –

回答

0

您不能真正將祖先對象的屬性附加到子對象上。子對象只能包含屬於它的屬性。您可以嘗試返回一個匿名對象,該對象具有您從CompanyOfficer加上MatchMethod屬性所需的所有屬性。例如:

var officers = db.RootDomains 
           .SelectMany(rd => rd.Companies) 
           .SelectMany(c => c.CompanyMatches) 
           .Select(cm => cm.CompaniesHouseRecord) 
           .SelectMany(chr => chr.CompanyOfficers) 
           .Select(x => new 
            { 
             Id = x.Id, // This would be the Company Officer Id 
             MatchMethod = x.CompanyHouseRecord.CompanyMatch.MatchMethod, // This would be the ancestor property you want to include 
             AnyOtherProperty = ... 
            }; 

這會給你一個IEnumerable對象的所有你想要的屬性。沒有辦法動態地將屬性添加到強類型對象恢復器。

+0

'x.CompanyHouseRecord.CompanyMatch.MatchMethod'不起作用,因爲它們不是單個集合。 – Guerrilla