我对结果有一些问题,我在连接上得到重复而不是两个具有不同值的重复项。 db包含这两个值,但其中一个在文本字段中有不同的值,为什么我得到重复,唯一相同的是CompanyPageId)。
var query = (from pageEducation in _context.PageEducations
join companyPage in _context.Pages on pageEducation.CompanyPageId equals companyPage.Id into p
from companyPage in p.DefaultIfEmpty()
where pageEducation.PageId == pageId
select new PageEducation
{
Id = pageEducation.Id,
PageId = pageEducation.PageId,
CompanyPageId = pageEducation.CompanyPageId,
CustomCompany = pageEducation.CustomCompany,
CompanyPage = companyPage != null ? new Page {Name = companyPage.Name, Id = companyPage.Id} : null,
Education = pageEducation.Education
}).ToList();
我的表格如下:
CompanyPageId, education
33 edu1
33 edu2
,结果是包含两个项目的列表,但重复。
当我在SQL中运行此查询时,我不会得到任何重复项:
select * from Page_Educations
left join Pages on page_Educations.CompanyPageId = pages.Id
where page_Educations.PageId = 10
当我在LinqPad中运行linq时,它甚至可以正常工作
var query = from pageEducation in Page_Educations
join companyPage in Pages on pageEducation.CompanyPageId equals companyPage.ID into p
from companyPage in p.DefaultIfEmpty()
where pageEducation.PageId == 10
select new
{
Id = pageEducation.Id,
PageId = pageEducation.PageId,
CompanyPageId = pageEducation.CompanyPageId,
CustomCompany = pageEducation.CustomCompany,
CompanyPage = companyPage != null ? new {Name = companyPage.Name, Id = companyPage.ID} : null,
Education = pageEducation.Education
};
query.Dump();