Given the following query:
var query = from item in context.Users // Users if of type TblUser
select new User() // User is the domain class model
{
ID = item.Username,
Username = item.Username
};
How can I reuse the selected part of the instruction in other queries? I.e.
var query = from item in context.Jobs // Jobs if of type TblJob
select new Job() // Job is the domain class model
{
ID = item.JobId,
User = ReuseAboveSelectStatement(item.User);
};
I tried using the mapper method:
public User MapUser(TblUser item)
{
return item == null ? null : new User()
{
ID = item.UserId,
Username = item.Username
};
}
WITH
var query = from item in context.Users
select MapUser(item);
But if I do this, then the framework throws an error, for example:
LINQ to Entities does not recognize the MapUser (TblUser) method, and this method cannot be translated into a storage expression.
source
share