我有一个多语言实体(翻译后的字符串存储在单独的表中),我想过滤当前 UI 文化的翻译后的字符串。如果当前 UI 文化没有翻译的字符串,我想获取并过滤实体的第一个默认翻译字符串。
我正在使用 .NET Core 2.1、EF Core 2.1 和 ABP Boilerplate 3.6.2 框架。
这是我的代码。此功能是过滤DocumentCategory横跨实体名称的财产DocumentCategoryTranslation相关实体。如果有的话,我采用当前的 UI 文化翻译字符串,否则我采用第一个。返回的对象只是我的结果的包装器。
public async Task<PagedResultDto<GetDocumentCategoryForView>> GetAll(GetAllDocumentCategoriesInput input)
{
var query = (from dc in _documentCategoryRepository.GetAllIncluding(dc => dc.Translations)
select new
{
dc,
t = dc.Translations.FirstOrDefault(t => t.Language == CultureInfo.CurrentUICulture.Name) ?? dc.Translations.First()
}).AsQueryable()
.WhereIf(!string.IsNullOrWhiteSpace(input.Filter), e => false || e.t.Name.ToLower().Contains(input.Filter.ToLower()));
var result = await query.ToListAsync();
return new PagedResultDto<GetDocumentCategoryForView>(
result.Count,
result.Select(o => new GetDocumentCategoryForView()
{
DocumentCategory = ObjectMapper.Map<DocumentCategoryDto>(o.dc)
}).ToList()
);
} …
Run Code Online (Sandbox Code Playgroud)