使用linq to sql include和3个分层数据级别将数据具体化为自定义dtos

时间:2017-04-27 10:19:03

标签: c# linq-to-sql

我有3个表在主要细节关系中链接,顶级组织表可以有多个站点,每个站点可以有一个或多个米。此代码工作正常,但如果我尝试将数据实现为自定义dtos,我无法访问第三级米数据。我收到错误

' ICollection不包含Meters'

的定义

但是有米数据存在,谁能告诉我哪里出错?

var overView = await (from organisation in DbContext.Organisations select organisation)
                .Where(o => o.ID == request.Id)
                .Include(q => q.Sites.Select(t => t.Meters))
                .Select(item => new OrganisationAggregateDto
                {
                    Organisation = new OrganisationDto { Address1 = item.Address1, Address2 = item.Address2, Address3 = item.Address3, City = item.City, County = item.County, EmailFrom = item.EmailFrom, EmailIsSSL = item.EmailIsSSL, EmailPortNumber = item.EmailPortNumber, EmailServerHost = item.EmailServerHost, EmailServerUserName = item.EmailServerUserName, End_User_Org = item.End_User_Org, GroupId = item.GroupId, ID = item.ID, IsRetail = item.IsRetail, OrganisationName = item.OrganisationName, ParentOrganisationID = item.ParentOrganisationID, Postcode = item.Postcode, Role = item.Role, Status = item.Status, UserId = item.UserId, ParentOrganisationName = "", IsRetailNarrative = item.IsRetail == true ? "Yes" : "No", ContractType = item.ContractType, EmailAddress = item.EmailAddress },
                    Sites = item.Sites.Select(s => new Dto.Site.SiteDto { ADDRESS1 = s.ADDRESS1, ADDRESS2 = s.ADDRESS2, ADDRESS3 = s.ADDRESS3, ADDRESS4 = s.ADDRESS4, ADDRESS5 = s.ADDRESS5, End_User_Org = s.End_User_Org, ID = s.ID, POSTCODE = s.POSTCODE, LOCATION_NAME = s.LOCATION_NAME, COMMODITY = s.COMMODITY, CONTACT_EMAIL = s.CONTACT_EMAIL, CONTACT_FAX = s.CONTACT_FAX, CONTACT_NAME = s.CONTACT_NAME, CONTACT_TEL = s.CONTACT_TEL, COUNTRY = s.COUNTRY, CUSTOMER_LOCATION_ID = s.CUSTOMER_LOCATION_ID, EAC = s.EAC, SUPPLY_CAPACITY_UOM = s.SUPPLY_CAPACITY_UOM, OrgId = s.OrgId, VOLTAGE = s.VOLTAGE }).ToList(),
                    Meters = item.Sites.Meters(m => new Dto.Meter.MeterDto { DURATIONTYPE = m.DURATIONTYPE, ID = m.ID, METER_TYPE = m.METER_TYPE, MPAN = m.MPAN, SiteID = m.SiteID, UNIT_OF_MEASURE = m.UNIT_OF_MEASURE, CAPACITY = m.CAPACITY, CUSTOMER_LOCATION_ID = m.CUSTOMER_LOCATION_ID, DFLT_DF_MR_DURATION_TYPE = m.DFLT_DF_MR_DURATION_TYPE, METER_SUPPLEMENTARY = m.METER_SUPPLEMENTARY }).ToList(),
                    ID = item.ID,
                    Contracts = null
                }).FirstOrDefaultAsync();

0 个答案:

没有答案