Invoking an expression in a Select expression - LINQ to Entity Framework

I'm trying to use the existing Building Expression class that I made when I tried to make a select clause, but I'm not sure how to attach the expression to the expression tree for Select, I tried to do the following

var catalogs = matchingCatalogs.Select(c => new
                {
                    c.CatalogID,
                    Name = EntitiesExpressionHelper.MakeTranslationExpression<Catalog>("Name", ApplicationContext.Instance.CurrentLanguageID).Compile().Invoke(c),
                    CategoryName = EntitiesExpressionHelper.MakeTranslationExpression<Category>("Name", ApplicationContext.Instance.CurrentLanguageID).Compile().Invoke(c.Category),
                    c.CategoryID,
                    c.StartDateUTC,
                    c.EndDateUTC
                });

But I obviously get the message that the Entity Framework cannot map Invoke to the SQL method. Is there any way around this?

FYI, EntitiesExpressionHelper.MakeTranslationExpression <T> (string name, int languageID) is equivalent to:

x => x.Translations.Count(t => t.LanguageID == languageID) == 0 ? x.Translations.Count() > 0 ? x.Translations.FirstOrDefault().Name : "" : x.Translations.FirstOrDefault(t => t.LanguageID == languageID).Name

EDIT: I understand that I need to use ExpressionVisitor for this, but I'm not sure how to use ExpressionVisitor to change the MemberInitExpression expression, so if anyone knows how to do this, let me know.

+3
1

vars. . , :

Expression<Func<Foo, Bar>> exp = GenExpression();
var q = matchingCatalogs.Select(exp);

:

var q = matchingCatalogs.Select(GenExpression());

GenExpression L2E. GenExpression L2E, .

, var , . , .

0

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


All Articles