Adalat  Khan

Adalat Khan

  • 652
  • 1.5k
  • 865.1k

Unmapped field trigger an exception when i use it in LINQ query

May 23 2023 9:41 AM

Hi,

I recently migrated my project from .NET Core 3.1 to .NET 6.0. In .NET Core 3.1 it was working perfectly but after migration i have one error in the LINQ query. Actually i have one Entity model "Employee". In the Employee entity model there is one filed which is unmapped into database and it is only getter. Following is my Employee entity model:

public class Employee {

public int Id {get; set;}
public string EmployeeReference {get; set;}
public string EmployeeFullName {get; set;}
public string EmployeePhoneNumber {get; set;}

[NotMapped]
public bool IsHQ 
{
get
{
return this is HQEmployee;   // HQ means Head Quearter
}
}
}

My project is based on Generic Repository pattern. Following is the Interface of my Generic Repository:

public interface IRepository<T>
{
IQuerable<T> All {get;}
T Find(int id);
IQuerable<T> GetIncluding(params Expression<Func<T, object>>[] includesproperties);
}

The folowing Interface is inherited from IRepository Interface:

public interface IEmployeeRepository : IRepository<Employee>
{
}

Following is the method where i want to retrieve those Employees who belongs to Head Quearters:

public class GetEmployeesListQuery : IGetEmployeesListQuery
{
private readonly IEmployeeRepository _employeeRepository;

public GetEmployeesListQuery(IEmployeeRepository employeeRepository)
{
_employeeRepository = employeeRepository;
}

public List<GetEmployeAuthoritiesListModel> GetEmployeeAuthoritiesList()
{
var employeeList = new List<GetEmployeAuthoritiesListModel>();

var allEmployees = _employeeRepository.GetIncluding(e => e.KBZPosition, e => e.SourceOrganization, e => e (HQEmployee).Rank).Where(e => e.IsHQ);
}
}

I am getting error in the Where Clause of the LINQ Query becuase i am checking the unmapped field "IsHQ". The error says that the field "IsHQ" is not mapped into database. Following is the full description of the 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.

I have searched on google and find this question in the net but i did not find a solution for my issue. Please give me suggestion because it was working in .NET Core 3.1 and the project is already in production.


Answers (2)