0

I have a generic repository with a method like this

   public async Task<List<TEntity>> Get(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, Expression<Func<TEntity, TEntity>> select=null)
    {
        IQueryable<TEntity> query = dbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

        if (select != null)
        {
            query = query.Select(select);
        }
        if (orderBy != null)
        {
            return await orderBy(query).ToListAsync();
        }
        else
        {
            return await query.ToListAsync();
        }
    }

and use this in my repository like this:

return await _unitOfWork.GetRepository<DeliveryMethod>().
                Get(q => q.DeliveryMethodLoclizes.Any(x => x.LanguageId == language), null,
                    s => new DeliveryMethod()
                    {
                        Id = s.Id,
                        IsActive = s.IsActive,
                        IsDel = s.IsDel,
                        DeliveryMethodLoclizes = s.DeliveryMethodLoclizes.Where(x => x.LanguageId == language)
                            .Select(w => new DeliveryMethodLocalize()
                            {
                                Id = w.Id,
                                LanguageId = w.LanguageId,
                                Title = w.Title,
                                Description = w.Description
                            })
                            .ToList()
                    });

but when I run my code return this error to me

The entity or complex type 'Domain.DeliveryMethod' cannot be constructed in a LINQ to Entities query.

please help me for resolve this error

Majid Dehnamaki
  • 174
  • 1
  • 2
  • 13

1 Answers1

1

This is by design, EF doesn't allow you to project the results of a query onto a mapped entity. You can do use a DTO or anonymous type which doesn't inherit from the mapped entity