使用 Automapper 基于 List c# 和 linq 中的 List 进行有效过滤

我有以下型号;


public class Category : Entity

{

    public List<CategoryTranslation> Translations { get; set; }

    public int Value { get; set; }

    public bool AutoTranslate { get; set; }

}

在哪里


public class CategoryTranslation : Entity

{

    public string Name { get; set; }

    public Language Language { get; set; }

    public bool PrimaryTranslation { get; set; }

}


public class Language : Entity

{

    public string Country { get; set; }

    public string Code { get; set; }

    public bool? IsPrimary { get; set; }

    public bool IsActive { get; set; }

}

这个想法是我们可以存储类别的各种翻译。


在我们的 API 上,我希望为特定语言提供输出模型;


CategoryOutputModel {

     public int Id;

     public string Name;

}

其中 name 是从Category.Translations.Name.


我是否可以选择具有特定翻译的所有类别,然后只选择该翻译并处理翻译列表中不需要的元素


IE


//this would return all the categories i need but include all tranlsations and not just the 'en' ones

categories.Where(x => x.Translations.Any(y => y.Language.Code == "en"));

然后我会寻找用于Automapper将返回的数据(从我的服务)映射到我的输出模型并返回。


我假设我需要进行过滤,以便 Automapper 知道如何映射到该Name字段?


蝴蝶不菲
浏览 386回答 1
1回答

翻翻过去那场雪

您可能必须在CategoryTranslationset 而不是Categoryset上进行过滤,然后再GroupBy进行过滤Category。在实体上设置导航属性public class Category : Entity{&nbsp; &nbsp; public int Value { get; set; }&nbsp; &nbsp; public bool AutoTranslate { get; set; }&nbsp; &nbsp; public List<CategoryTranslation> Translations { get; set; }}public class CategoryTranslation : Entity{&nbsp; &nbsp; public string Name { get; set; }&nbsp; &nbsp; public bool PrimaryTranslation { get; set; }&nbsp; &nbsp; public int CategoryId { get; set; }&nbsp; &nbsp; public Category Category { get; set; }&nbsp; &nbsp; public int LanguageId { get; set; }&nbsp; &nbsp; public Language Language { get; set; }}public class Language : Entity{&nbsp; &nbsp; public string Country { get; set; }&nbsp; &nbsp; public string Code { get; set; }&nbsp; &nbsp; public bool? IsPrimary { get; set; }&nbsp; &nbsp; public bool IsActive { get; set; }&nbsp; &nbsp; public List<CategoryTranslation> CategoryTranslations { get; set; }}配置他们的关系public class AppDbContext : DbContext{&nbsp; &nbsp; public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; protected override void OnModelCreating(ModelBuilder builder)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; base.OnModelCreating(builder);&nbsp; &nbsp; &nbsp; &nbsp; builder.Entity<Category>(b =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.HasKey(x => x.Id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.ToTable("Category");&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; builder.Entity<CategoryTranslation>(b =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.HasKey(x => x.Id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.Property(x => x.Name).IsRequired();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.HasOne(x => x.Category)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .WithMany(c => c.Translations)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .HasForeignKey(x => x.CategoryId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.HasOne(x => x.Language)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .WithMany(l => l.CategoryTranslations)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .HasForeignKey(x => x.LanguageId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.ToTable("CategoryTranslation");&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; builder.Entity<Language>(b =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.HasKey(x => x.Id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.Property(x => x.Country).IsRequired();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.Property(x => x.Code).IsRequired();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.ToTable("Language");&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; public DbSet<Category> Categories { get; set; }&nbsp; &nbsp; public DbSet<CategoryTranslation> CategoryTranslations { get; set; }&nbsp; &nbsp; public DbSet<Language> Languages { get; set; }}启动时的种子表&nbsp; &nbsp; public void Configure(IApplicationBuilder app, AppDbContext dbContext)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; SeedData(dbContext);&nbsp; &nbsp; &nbsp; &nbsp; app.UseStaticFiles();&nbsp; &nbsp; &nbsp; &nbsp; app.UseMvcWithDefaultRoute();&nbsp; &nbsp; }&nbsp; &nbsp; private void SeedData(AppDbContext dbContext)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; dbContext.Database.EnsureDeleted();&nbsp; &nbsp; &nbsp; &nbsp; dbContext.Database.Migrate();&nbsp; &nbsp; &nbsp; &nbsp; Language english = new Language&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsActive = true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsPrimary = true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Country = "United States",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Code = "en-US"&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; Language traditionalChinese = new Language&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsActive = true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsPrimary = false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Country = "Chinese (Taiwan)",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Code = "zh-TW"&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; Language simplifedChinese = new Language&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsActive = true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsPrimary = false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Country = "Chinese (People's Republic of China)",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Code = "zh-CN"&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; Language korean = new Language&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsActive = true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsPrimary = false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Country = "Korea",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Code = "ko-KR"&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; Language japanese = new Language&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsActive = true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsPrimary = false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Country = "Japan",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Code = "ja-JP"&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; Category guitar = new Category&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value = 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AutoTranslate = true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Translations = new List<CategoryTranslation>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new CategoryTranslation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "Guitars",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Language = english,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PrimaryTranslation = true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new CategoryTranslation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "吉他",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Language = traditionalChinese,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PrimaryTranslation = false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new CategoryTranslation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "吉他",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Language = simplifedChinese,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PrimaryTranslation = false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new CategoryTranslation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "기타",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Language = korean,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PrimaryTranslation = false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new CategoryTranslation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "ギター",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Language = japanese,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PrimaryTranslation = false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; Category bass = new Category&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value = 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AutoTranslate = true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Translations = new List<CategoryTranslation>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new CategoryTranslation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "Bass",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Language = english,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PrimaryTranslation = true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new CategoryTranslation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "低音吉他",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Language = traditionalChinese,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PrimaryTranslation = false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new CategoryTranslation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "低音吉他",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Language = simplifedChinese,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PrimaryTranslation = false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; dbContext.Categories.AddRange(guitar, bass);&nbsp; &nbsp; &nbsp; &nbsp; dbContext.Languages.AddRange(english, traditionalChinese,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; simplifedChinese, korean, japanese);&nbsp; &nbsp; &nbsp; &nbsp; dbContext.SaveChanges();&nbsp; &nbsp; }验证数据库设置 AutoMapper 配置文件以映射Category到CategoryOutputModelpublic class AutoMapperProfile : Profile{&nbsp; &nbsp; public AutoMapperProfile()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; CreateMap<Category, CategoryOutputModel>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ForMember(dest => dest.Name, opt => opt.ResolveUsing(src =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Because of your setup, it doesn't guarantee that&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // there is only one translation came out at the end for a&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // language code for a category so I used FirstOrDefalt()&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // here.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; src.Translations.FirstOrDefault()?.Name));&nbsp; &nbsp; }}读public class HomeController : Controller{&nbsp; &nbsp; private readonly AppDbContext _dbContext;&nbsp; &nbsp; private readonly IMapper _mapper;&nbsp; &nbsp; public HomeController(AppDbContext dbContext,&nbsp; &nbsp; &nbsp; &nbsp; IMapper mapper)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _dbContext = dbContext;&nbsp; &nbsp; &nbsp; &nbsp; _mapper = mapper;&nbsp; &nbsp; }&nbsp; &nbsp; public IActionResult Index()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var categoryTranslations = _dbContext.CategoryTranslations&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .AsNoTracking()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Include(ct => ct.Category)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Include(ct => ct.Language)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(ct => ct.Language.Code == "en-US")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ToList();&nbsp; &nbsp; &nbsp; &nbsp; var categoryOutputModels = categoryTranslations&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GroupBy(ct => ct.Category, (key, group) =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Use this map overload to map the category entity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // to a new CategoryOutputModel object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _mapper.Map<Category, CategoryOutputModel>(key));&nbsp; &nbsp; &nbsp; &nbsp; return View();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP