2017-01-22 235 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