How do you reuse select commands with Entity Framework?

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 // Users if of type TblUser
            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.

+3
source share
2 answers

. LINQ , SQL.

, , linqkit ( ) , m , , , , .

, : IMHO - ? , , EF ...

+1

MapUser static.

0

Source: https://habr.com/ru/post/1757578/


All Articles