I have created an automapper
CreateMap<Records_Log, Records_LogDto>()
.ForMember(dest => dest.UserName, opt =>
{
opt.ResolveUsing(d => d.User.UserName);
});
Records_Log
public class Records_Log
{
public int Id { get; set; }
public string Table_name { get; set; }
[ForeignKey("Records")]
public int RecordsId { get; set; }
public Records Records { get; set; }
public int RecordId { get; set; }
public string OldValue { get; set; }
public string NewValue { get; set; }
[ForeignKey("User")]
public string UserId { get; set; }
public ApplicationUser User { get; set; }
public DateTime ChangeDate { get; set; }
public string Action { get; set; }
}
Records_LogDto
public class Records_LogDto
{
public int Id { get; set; }
public string Table_name { get; set; }
public int RecordsId { get; set; }
public Records Records { get; set; }
public int RecordId { get; set; }
public string OldValue { get; set; }
public string NewValue { get; set; }
public string UserId { get; set; }
public string UserName { get; set; }
public string Action { get; set; }
public DateTime ChangeDate { get; set; }
}
Records:
public class Records
{
public int Id { get; set; }
public string SerialName { get; set; }
public string RecordsName { get; set; }
public string Url { get; set; }
public string AttachmentPath { get; set; }
public string ImagePath { get; set; }
public string RecordsNameModified { get; set; }
public bool isDeleted { get; set; }
public bool isRead { get; set; }
public bool IsHidden { get; set; }
public bool MarkAsImportant { get; set; }
public string ImportantComments { get; set; }
public int subTypeID { get; set; }
public virtual ICollection<Records_Rate> Records_Rate { get; set; }
}
My query
var query = (from st in db.Records_State
join sl in db.Records_Log
on st.Records.Id equals sl.RecordsId
where st.Records.isDeleted
&& sl.Table_name == nameof(Records)
&& sl.Action == "Deleted"
&& sl.NewValue == "Top"
&& (string.IsNullOrEmpty(recordsName) || sl.Records.RecordsName.Contains(recordsName)
|| sl.Records.RecordsNameModified.Contains(recordsName))
&& (fromDate == null || sl.ChangeDate >= fromDate)
&& (toDate == null || sl.ChangeDate <= toDate)
orderby sl.ChangeDate descending
select new Records_Log {
Records= st.Records,
ChangeDate= sl.ChangeDate
})
.AsQueryable();
query = from p in query
group p by p.Records.Id
into pGroup
orderby pGroup.Key
select pGroup.FirstOrDefault();
var result = query.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.OrderByDescending(st => st.Id);
var dto = Mapper.Map<List<Records_LogDto>>(result);
now in the last line var dto = Mapper.Map<List<Records_LogDto>>(result);
it throws exception:
Error mapping types.\r\n\r\nMapping types:\r\nDbQuery1 -> List1\r\nSystem.Data.Entity.Infrastructure.DbQuery1[[PortalBackEndAPI.Models.Records_Log, PortalBackEndAPI.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.List1[[PortalBackEndAPI.Dto.Records_LogDto, PortalBackEndAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"}
and
The entity or complex type 'PortalBackEndAPI.Models.Records_Log' cannot be constructed in a LINQ to Entities query