Kamran Khan
Kamran Khan

Reputation: 33

I want to get values in List EF

I have table i am using two foreign key of different table every time i insert value one of them will be null. I want to get the record in List using EF it gives me the following error

The cast to value type 'System.Int64' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.

Here is my Code

public static List<ShowCartModelView> ViewAllCarts()
    {
        try
        {
            var userid = HttpContext.Current.Session["UserID"];
            var id = (long)userid;
            using (var db = new DatabaseContext())
            {
                var model =
                    db.Carts
                        .Where(x => x.memberId == id)
                        .Select(x => new ShowCartModelView
                        {
                            CardId = x.CardId,
                            ProductId = x.ProductId,
                            AccountType = x.UserDumps.accountType,
                            DumpsCountry = x.UserDumps.country,
                            CardsCountry = x.UserCards.country,
                            CardName = x.UserCards.name,
                            CardsBin = x.UserCards.nummber,
                            DumpsBin = x.UserDumps.bin,
                            DumpsBank = x.UserDumps.Bank,
                            DumpsPrice = x.UserDumps.price,
                            CardsPrice = x.UserCards.Price,
                            CartType = x.CartType

                        }).ToList();

                return model;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

I want to show record in list view Please help me with this.

Upvotes: 0

Views: 327

Answers (1)

Timothy Stepanski
Timothy Stepanski

Reputation: 1196

Your entity has an Int64 or long defined while the respective column is nullable, change the datatype to: long?

Upvotes: 1

Related Questions