c# - 查询源已与表达式关联

标签 c# asp.net-core aspnetboilerplate ef-core-2.0

我有一个多语言实体(翻译后的字符串存储在单独的表中),我想过滤当前 UI 文化的翻译后的字符串。如果当前 UI 文化没有翻译的字符串,我想获取并过滤实体的第一个默认翻译字符串。

我正在使用 .NET Core 2.1、EF Core 2.1 和 ABP Boilerplate 3.6.2 框架。

这是我的代码。此函数正在过滤 文档分类 实体跨越姓名 的属性(property)文档类别翻译相关实体。如果有的话,我采用当前的 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()
    );
}

我得到的异常(exception):

System.InvalidOperationException: 'Query source (from DocumentCategoryTranslation t in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[Colnec.Documents.DocumentCategoryTranslation])) has already been associated with an expression.'



如果我删除 GetAllInducing(dc => dc.Translations) 查询正在运行,但在函数末尾完成的映射会引发空异常(因为它使用了 dc 实体的 Translations 属性)。

automapper 扩展功能(来自 ABP 框架)。它设置了 姓名 目标对象的属性到当前 UI 区域性翻译字符串。 翻译 属性需要在这里加载。

public static void CreateMultiLingualMapFromEntity<TMultiLingualEntity, TMultiLingualEntityPrimaryKey, TTranslation, TDestination>(
    this IMapperConfigurationExpression configuration, MultiLingualMapContext multiLingualMapContext)
    where TTranslation : class, IEntityTranslation<TMultiLingualEntity, TMultiLingualEntityPrimaryKey>
    where TMultiLingualEntity : IMultiLingualEntity<TTranslation>
{
    configuration.CreateMap<TTranslation, TDestination>();

    configuration.CreateMap<TMultiLingualEntity, TDestination>().BeforeMap((source, destination, context) =>
    {
        var translation = source.Translations.FirstOrDefault(pt => pt.Language == CultureInfo.CurrentUICulture.Name);
        if (translation != null)
        {
            context.Mapper.Map(translation, destination);
            return;
        }

        var defaultLanguage = multiLingualMapContext.SettingManager
                                                    .GetSettingValue(LocalizationSettingNames.DefaultLanguage);

        translation = source.Translations.FirstOrDefault(pt => pt.Language == defaultLanguage);
        if (translation != null)
        {
            context.Mapper.Map(translation, destination);
            return;
        }

        translation = source.Translations.FirstOrDefault();
        if (translation != null)
        {
            context.Mapper.Map(translation, destination);
        }
    });
}

我尝试了很多不同的事情,但我不知道如何正确处理这种情况。如果你们中的一些人对此有一个很好的解决方案,我将非常感谢您的帮助。

祝你有美好的一天!
亚历山大

最佳答案

看起来 EF 由于未知原因无法处理此查询,因此我会尝试走查询简化路线,以免压倒 EF。如果我了解您的用例,您希望按名称翻译过滤类别。看看下面的片段是否有意义以及它是否适用于您的情况。

string filterValue = input.Filter?.ToLower();
string defaultCultureName = "?"; // todo

var query = _documentCategoryRepository.GetAllIncluding(dc => dc.Translations)
            .WhereIf(!string.IsNullOrWhiteSpace(filterValue), e => false || 
                e.Translations.Any(t => 
                    (t.Language == CultureInfo.CurrentUICulture.Name && t.Name.ToLower().Contains(filterValue)) ||
                    (t.Language == defaultCultureName && t.Name.ToLower().Contains(filterValue)));

var result = await query.ToListAsync();

return new PagedResultDto<GetDocumentCategoryForView>(
    result.Count,
    result.Select(dc => new GetDocumentCategoryForView()
    {
      DocumentCategory = ObjectMapper.Map<DocumentCategoryDto>(dc)
    }).ToList()
);

关于c# - 查询源已与表达式关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50694050/

相关文章:

c# - 为什么 web.config 是在发布时创建的,而不是在 Asp.Net Core API 中构建项目时创建的?

asp.net - 如何修改 launchSettings.json 以指向 index.html

asp.net-core - .NET Core (ASP MVC) 在构建后运行

visual-studio - ESlint : Expected ! == 而是看到了 !=

c# - 获取 asp.net 样板存储库使用的 DbContext 实例

c# - HTML 选择器库

具有不同数据库的C#项目,以不同​​格式存储相同数据

c# - 仅针对一个 namespace 解析 XML

c# - StringTemplate 中的嵌套循环

c# - 使用 WhereIf 时找不到 .ToListAsync()