error when returning a generic list linq

2

I have the following problem, when returning a list generated in a method for a viewbag it throws an error to me at the moment of the return, I put the image to them so that they see where the error throws me

    public List<Card> GetCard()
    {
        var user = db.Usuarios.Where(u => u.Email == User.Identity.Name).FirstOrDefault();
        var card = (from Cards in db.Cards.Where(u => u.Id_Usuario == user.Id_Usuario)
                    select new Card
                    {
                        CardNumber = Cards.CardNumber.Replace((Cards.CardNumber.Substring(1 - 1, 12)), "************"),
                        IdCard = Cards.IdCard,
                    }).ToList();

        card.Add(new Card
        {
            IdCard = 0,
            CardNumber = "[Seleccione una tarjeta...]",
        });

        return card.OrderBy(a => a.CardNumber).ToList();
    }

this is the error that is indicating me this is

SisVentaOnlinne.Models.Card' cannot be constructed in a LINQ to Entities query.

help

    
asked by Ing. Jose Valera 16.04.2018 в 17:03
source

1 answer

2

You have two options

If you want to return those two attributes in a list you can create a class eg CardRetornado

    public class CardRetornado
    {
        public  string CarNumber { get; set; }
        public int IdCard { get; set; }
    }
     

and in your controller instead of returning Card returns CardRetornado like this:

public List<CardRetornado> GetCard()
{
    var Card = (from Cards in db.cards.Where(u => u.id_usuario = User.id_isuario) select new CardRetornado { CarNumber= tucodigo ,IdCard.IdCard }).tolist();
    return Card.OrderBy(a => a.Cardnumer).Tolist();
}

Or if you want to return the whole object Card

public List<Card> GetCard()
{
    var Card = (from Cards in db.cards.Where(u => u.id_usuario = User.id_isuario) select Cards).tolist();
    return Card.OrderBy(a => a.Cardnumer).Tolist();
}
    
answered by 16.04.2018 в 17:23