3

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.

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.

defect833
  • 275
  • 7
  • 22
  • 1
    Iļø would edit your question to be clearer on what you are asking. As you see from the one answer he is thinking you want only Filename property – dinotom Nov 15 '17 at 08:34
  • Possible duplicate of [Exclude a column from a select using LINQ](https://stackoverflow.com/questions/19463099/exclude-a-column-from-a-select-using-linq) – Michael Freidgeim Mar 29 '18 at 11:10

1 Answers1

3

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();
Backs
  • 24,430
  • 5
  • 58
  • 85
  • While this answer was not exactly the solution I was after, Dto's were the concept that I needed. My solution required more than just the FileName so I changed Docs to the following: Docs = e.EmploymentDocuments.Select(o => new EmploymentBinary { BinaryFileID = o.BinaryFileID, FileName = o.FileName }) – defect833 Nov 16 '17 at 00:28