Reusing an expression in a Linq select expression (query format)

I have an expression that converts one type of object to another type. The expression is as follows:

public Expression<Func<SQLRepository.ActionType, Model.ActionType>> DBActionTypeToActionType =
(SQLRepository.ActionType at) => new Model.ActionType()
{
    ID = at.OID,
    DisplayName = at.DisplayName
};

I can use the expression this way:

var linq = (from at in dc.SQLRepositoryDC.ActionTypes select at).Select(DBActionTypeToActionType);

But I would like to use it as follows:

var linq = (from at in dc.SQLRepositoryDC.ActionTypes select DBActionTypeToActionType.Compile().Invoke(at));

I searched for a couple of days and I can find links to this in the Where clause. It seems that if I can use function calls for this, it should be possible using the query syntax.

The reason it is important to use the query syntax is that some of the selected objects are made up of many sub-objects and trying to associate them with all the transformations along with the function designation will be much more difficult to write and maintain.

+3
1

, , .

. .

from x in y select z

y.Select(x => z)

, , Select, , .

, . , :

var query = from foo in dc.ActionTypes.Select(DBActionTypeToActionType)
            where foo.Stuff
            select foo.Other;

:

var query = (from bar in dc.ActionTypes
             where bar.Stuff
             select bar).Select(DBActionTypeToActionType);

?

+4

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


All Articles