Hi,
My project is based on Repository pattern. I have one entity model Employee which has one unmapped property IsGHQ. In the LINQ query i am comparing this unmapped property that's why its give me the following error:
"The linq expression could not be translated. Either rewrite the query in a form that can be translated or switch to client evaluation explicitly by inserting a call to AsEnumerable, ToList, or ToListAsync".
Even if i covert the linq query by add adding AsEnumberable but still i am getting the same error. Please help me.
Following is my LINQ Query where i am getting the error:
public CheckEmployeeExistanceModel Execute(int EmployeeNumber)
{
var empDetail = _employeeRepository.FindByInclude(a => a.IsGHQ && (a as GHQEmployee).EmployeeNumber == EmployeeNumber, e => (e as GHQEmployee).Rank, e => (e as GHQEmployee).title).FirstOrDefault();
if(empDetails != null)
{
return new CheckEmployeeExistanceModel()
{
EmployeeReference = empDetails.EmployeeReference,
FulName = empDetails.FullRankTitleEnglishShort + "/" + empDetail.FullNameEnglish
};
}
}I am getting the above error in line number 3 when the query is executing.
Following is my Repository class:
public class Repository : IRepository where T : class, IEntity
{
protected DbContext _context;
internal DbSet _dbSet;
public Repository(IPersistanceContext persistanceContext)
{
_context = persistanceContext.Context;
_dbSet = _context.Set(0;
}
public virtual IQuerable All
{
return _context.Set().AsNoTracking();
}
public IQuerable FindIncluding(params Expression>[] includeProperties)
{
IQuerable querable = All;
return includeProperties.Aggregate(querable, (current, includeProperty) => current.Include(includeProperty));
}
public IQuerable FindByInclude(Expression> predicate, params Expression>[] includeProperties)
{
var query = FindIncluding(includeProperties);
return return query.Where(predicate);
}
}Following my Employee Entity Model:
public class Employee : IEntity
{
public int Id {get; set;}
public string EmployeeReference {get; set;
public string FullRankTitleEnglishShort {get; set;
public string FullNameEnglish {get; set;}
-------
-------
-------
[NotMapped]
public bool IsGHQ
{
get
{
return this is GHQEmployee;
}
}
}Following is my GHQEmployee Entity:
public class GHQEmployee : Employee
{
public int EmployeeNumber {get; set;}
public Title Tilte {get; Set;}
public Rank Rank {get; set;}
----------
----------
----------
}

Mohammad HussainRank #513 · 4.8K reputationPosted Sep 12, 2023, 3:58 AM EDT
The error you're encountering is due to the fact that you're trying to use an unmapped property,
IsGHQ, in your LINQ query. Entity Framework Core cannot translate the logic involving this property into SQL because it's not mapped to a database column. To work around this issue, you can modify your query to first filter by the type of entity (EmployeeorGHQEmployee) and then apply the additional filter onEmployeeNumber.Here's how you can rewrite your LINQ query:
public CheckEmployeeExistanceModel Execute(int EmployeeNumber) { var empDetail = _employeeRepository .FindByInclude(e => e.EmployeeNumber == EmployeeNumber && e is GHQEmployee, e => (e as GHQEmployee).Rank, e => (e as GHQEmployee).Title) .FirstOrDefault(); if (empDetail != null) { return new CheckEmployeeExistanceModel() { EmployeeReference = empDetail.EmployeeReference, FullName = empDetail.FullRankTitleEnglishShort + "/" + empDetail.FullNameEnglish }; } // Handle the case where empDetail is null (not found). // You may want to return null or throw an exception, depending on your requirements. return null; }In this revised query:
FindByIncludeto filter the entities based onEmployeeNumberand type (GHQEmployee).empDetailis not null before creating theCheckEmployeeExistanceModel.This approach should help you avoid the error related to the unmapped property and allow you to filter based on the
EmployeeNumberand entity type.Tahir AnsariRank #366 · 6.8K reputationPosted Sep 12, 2023, 3:58 AM EDT
Please check this
1) var empDetail = _employeeRepository.FindByInclude(a => a.IsGHQ && (a as GHQEmployee).EmployeeNumber == EmployeeNumber, e => (e as GHQEmployee).Rank, e => (e as GHQEmployee).title).FirstOrDefault();
2) if(empDetails != null)
Adalat KhanRank #1,143 · 1.6K reputationPosted Sep 13, 2023, 3:24 AM EDT
Dear @Mohammad Hussain thank you so much. Its working. Appricate your help