我想在List EF中获取值

时间:2016-06-20 18:45:03

标签: c# asp.net-mvc entity-framework

我有表我每次使用两个不同表的外键时,其中一个值为null。我想使用EF获取List中的记录它给我以下错误

  

演员价值类型' System.Int64'失败,因为具体化值为null。结果类型的通用参数或查询必须使用可空类型。

这是我的代码

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;
        }
    }

我想在列表视图中显示记录请帮助我。

1 个答案:

答案 0 :(得分:1)

您的实体具有Int64或long定义,而相应的列可以为空,请将数据类型更改为:long?