【发布时间】:2016-12-10 21:31:21
【问题描述】:
我需要有关在表格中搜索匹配文本的搜索方法的帮助。 这可行,除了联接需要是 LEFT OUTER JOIN 否则如果任何表中缺少 pageId,我都不会得到任何结果。
此解决方案需要很长时间才能运行,如果有人可以帮助我提供更好的解决方案来处理此任务,我将不胜感激。
public async Task<IEnumerable<Result>> Search(string query)
{
var temp = await (from page in _context.Pages
join pageLocation in _context.PageLocations on page.Id equals pageLocation.PageId
join location in _context.Locations on pageLocation.LocationId equals location.Id
join pageSpecialty in _context.PageSpecialties on page.Id equals pageSpecialty.PageId
join specialty in _context.Specialties on pageSpecialty.SpecialtyId equals specialty.Id
where
page.Name.ToLower().Contains(query)
|| location.Name.ToLower().Contains(query)
|| specialty.Name.ToLower().Contains(query)
select new Result
{
PageId = page.Id,
Name = page.Name,
Presentation = page.Presentation,
Rating = page.Rating
}).ToListAsync();
var results = new List<Result>();
foreach (var t in temp)
{
if (!results.Exists(p => p.PageId == t.PageId))
{
t.Locations = GetLocations(t.PageId);
t.Specialties = GetSpecialties(t.PageId);
results.Add(t);
}
}
return results;
}
【问题讨论】:
-
你为什么加入?没有
Page.PageLocations之类的导航属性吗? -
@GertArnold 不,我只是一个没有导航属性的普通 poco 对象。你的意思是,如果我有导航属性,我就不必加入?
-
是的,我就是这个意思。强烈推荐。如果您有它们,则对大多数谓词使用
Any会更容易。 -
@GertArnold 好的,你能给我举个例子,说明在使用导航属性和任何属性时该方法的外观吗?谢谢
标签: c# linq search entity-framework-core