Adalat  Khan

Adalat Khan

  • 650
  • 1.5k
  • 863.1k

Repository Pattern LINQ Query conversion Issue

Sep 12 2023 7:48 AM

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<T> : IRepository<T> where T : class, IEntity
{
protected DbContext _context;
internal DbSet<T> _dbSet;

public Repository(IPersistanceContext persistanceContext)
{
_context = persistanceContext.Context;
_dbSet = _context.Set<T>(0;
}

public virtual IQuerable<T> All
{
return _context.Set<T>().AsNoTracking();
}

public IQuerable<T> FindIncluding(params Expression<Func<T, object>>[] includeProperties)
{
IQuerable<T> querable = All;
return includeProperties.Aggregate(querable, (current, includeProperty) => current.Include(includeProperty));
}

public IQuerable<T> FindByInclude(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] 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;}
----------
----------
----------
}

 


Answers (3)