EF Core LINQ从包含的实体中排除列 [英] EF Core LINQ exclude column from included entity

查看:667
本文介绍了EF Core LINQ从包含的实体中排除列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将ASP.Net Core 2.0与Entity Framework一起使用,并且试图将模型返回到包含Employment实体的页面,该页面还包含了EmploymentDocument实体.对于后者,我不想加载data(byte [])列,但我希望所有其他列,最重要的是FileName.

I'm using ASP.Net Core 2.0 with Entity Framework and I am trying to return a model to a page that contains the Employment entity with it's collection of EmploymentDocument entities also included. For the latter I do not want to load the data (byte[]) column but I do want all the other columns, most importantly FileName.

我具有的linq查询,其中加载了包括data列在内的所有内容:

The linq query I have which loads everything including the data column is:

var employment = await _context.Employment
    .Include(e => e.EmploymentDocuments) // will load all associated document data
    .SingleOrDefaultAsync(m => m.EmploymentID == id);

此处的目的是能够在页面上显示所有文档名称的列表,并带有链接,然后可以使用这些链接来下载所选文件的数据.

The purpose here is to be able to show a list of all the document names on the page with links that can then be used to download the data for the file selected.

推荐答案

手动选择所需的所有数据并将其存储在某些Dto对象中:

Select all data you need by hands and store it in some Dto object:

var employment = await _context.Employment
    .Where(m => m.EmploymentID == id)
    .Select(e => new EmploymentDto
    { 
        ID = e.EmploymentID,
        Docs = e.EmploymentDocuments.Select(o => o.FileName)
    })
    .SingleOrDefaultAsync();

这篇关于EF Core LINQ从包含的实体中排除列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆