I have Entity e.g. CategoryEntity, RubricEntity, CityEntity etc. I need to show dropdowns for such entities. This entities have different properties. All of them I need to convert to DropDownListItem to show as dropdown, so I think I can use such method to work with DB, but I getting exception
The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.
public static IQueryable<DropDownListItem> ToDropDownList<TSource>(IQueryable<TSource> query, Expression<Func<TSource, long>> value, Expression<Func<TSource, string>> text)
{
var valueLambda = value.Compile();
var textLambda = text.Compile();
return query.Select(x => new DropDownListItem
{
Value = (long) valueLambda(x),
Text = (string) textLambda(x)
});
}
I think that I can use something like this for it but don't understand how to make it using expressions and lambda.
As a result I want something like
ToDropDownList2<RubricEntity>(_service.RubricAsQueryable(), x => x.Id, x => x.DisplayName)
The problem is that Linq To Entities doesn't support compiled expressions, and you are compiled both expression and trying to invoke them. But LINQ is trying to parse them as expressions and translate them into SQL code, and it can't do it. And you want to pass both
text
andvalue
as different parameters, so you can't combine them.You can do like this:
But as for me it has't a lot of sense...
If you still want to pass them as separate parameters, then you can try combine them in runtime like this:
Basically we just create stub expression with constant values, and then replace constant values depending on type to expression that comes in paremeters. And then send replaced expression to Select method, so it finaly expression will be look something like: