In ASP.NET Core-6 Web API, I have these models:
public class LeaveApplication
{
public Guid Id { get; set; }
public Guid EmployeeId { get; set; }
public string ReferenceNumber { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public virtual Employee Employee { get; set; }
public virtual LeaveApproval LeaveApproval { get; set; }
}
public class LeaveApproval
{
public Guid Id { get; set; }
public bool? IsApproved { get; set; }
public DateTime? ApprovedDate { get; set; } = null;
public virtual ICollection<LeaveApprovalAttachment> LeaveApprovalAttachments { get; set; }
}
public class LeaveApprovalAttachment
{
public Guid Id { get; set; }
public Guid LeaveApprovalId { get; set; }
public string FileType { get; set; }
public string FileName { get; set; }
public string FilePath { get; set; }
public long? FileSize { get; set; }
public virtual LeaveApproval LeaveApproval { get; set; }
}
A LeaveApplication has one LeaveApproval, while a LeaveApproval can have many LeaveApprovalAttachments.
Based on parameter id, I want to get a filepath of a LeaveApprovalAttachment and download the document. So, I have this code:
public async Task<LeaveApplication> GetLeaveDocumentByIdAsync(Guid id)
{
var leave = await _dbContext.LeaveApplications
.Where(m => (bool)m.IsApproved)
.Include(x => x.LeaveApproval)
.ThenInclude(x => x.LeaveApprovalAttachments)
.FirstOrDefaultAsync(x => x.LeaveApproval.LeaveApprovalAttachments.Any(x => x.Id == id));
var path = Path.Combine(Directory.GetCurrentDirectory(), leave.LeaveApproval.LeaveApprovalAttachments.FilePath);
var memory = new MemoryStream();
using (var stream = new FileStream(path, FileMode.Open))
{
await stream.CopyToAsync(memory);
}
memory.position = 0;
var contentType = "APPLICATION/octet-stream";
var fileName = Path.GetFileName(path);
return File(memory, contentType, fileName);
}
But I got this error:
Error CS1061 'ICollection' does not contain a definition for 'FilePath' and no accessible extension method 'FilePath' accepting a first argument of type 'ICollection' could be found (are you missing a using directive or an assembly reference?)
and it highlights FilePath in leave.LeaveApproval.LeaveApprovalAttachments.FilePath
How do I resolve this?
Thanks