Query with PagedList

0

I'm using PagedList.Mvc for pagination of results.

But when doing the following action with the querys:

   public ActionResult Index(int? page, string searchString)
    {
        int pageSize = 10;
        int pageNumber = (page ?? 1);
        var query = db.Grupos.AsQueryable();

        if (!string.IsNullOrWhiteSpace(searchString))
        {
            searchString = searchString.ToUpper();
            query = query.Where(x => x.Nome.ToUpper().Contains(searchString));
        }
        query = query.OrderByDescending(x => x.Id);

        query = query.Select(x => new Grupo
        {
            Id = x.Id,
            Nome = x.Nome
        });
        if (Request.IsAjaxRequest())
        {
            return PartialView("Lista", query.ToPagedList(pageNumber,pageSize));
        }
        return View(query.ToPagedList(pageNumber,pageSize));
    }

I encounter the following error when executing query.ToPagedList (pageNumber, pageSize):

Additional information: The entity or complex type 'Projeto.Entity.Grupo' cannot be constructed in a LINQ to Entities query.

I would like my query to search only the Id and Name of this group

    
asked by anonymous 24.12.2014 / 17:32

1 answer

-1

Make friends:

query = query.Select(i=> new { Id = i.Id, Nome = i.Nome }).Where(x => x.Nome.ToUpper().Contains(searchString));
    
24.12.2014 / 18:27