LINQKit icon indicating copy to clipboard operation
LINQKit copied to clipboard

Not able to utilize AsExpandable

Open shyamal890 opened this issue 7 years ago • 2 comments

I have created an extension method like:

public static GetRows()
        {
            var to_ret = db.TableRows(x=> new TableRowModel(
            {
                TableRowId = x.TableRowId,
                Type = x.Type,
                Name = x.Name,
                CreatedAt = x.CreatedAt,
                ModifiedAt = x.ModifiedAt,
                Enums = x.Enums.AsExpandable.Select(y => y.ToEnumModel())
            });
            return to_ret;
        }

public static EnumModel ToEnumModel(this Enum x)
        {
            var to_ret = new EnumModel()
            {
                CFPId = x.CFPId,
                CreatedAt = x.CreatedAt,
                ModifiedAt = x.ModifiedAt,
            };
            return to_ret;
        }

This gives error saying:

LINQ to Entities does not recognize the method 'System.Linq.IQueryable1[Test.Models.Enum] AsExpandable[Enum](System.Linq.IQueryable1[Test.Models.Enum])' method, and this method cannot be translated into a store expression.

shyamal890 avatar Oct 25 '17 13:10 shyamal890

ToEnumModel must be an expression, for example:

public static Expression<Func<Enum, EnumModel>> ToEnumModel()
{
    return x => new EnumModel
    {
        CFPId = x.CFPId,
        CreatedAt = x.CreatedAt,
        ModifiedAt = x.ModifiedAt,
    };
}

and later:

Enums = x.Enums.AsExpandable.Select(ToEnumModel())

isc30 avatar Jan 17 '19 09:01 isc30

@shyamal890 it should be like this

public static GetRows()
        {
           var toEnumModel= ToEnumModel();
            var to_ret = db.TableRows.AsExpandable().Select(x=> new TableRowModel(
            {
                TableRowId = x.TableRowId,
                Type = x.Type,
                Name = x.Name,
                CreatedAt = x.CreatedAt,
                ModifiedAt = x.ModifiedAt,
                Enums = x.Enums.AsQuaryable().Select(y => toEnumModel.Invoke(y))
            });
            return to_ret;
        }

mhamri avatar Apr 18 '19 09:04 mhamri