Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误:转换为值类型';System.Int32';由于物化值为null,因此失败_C#_Asp.net Mvc_Entity Framework_Linq - Fatal编程技术网

C# 错误:转换为值类型';System.Int32';由于物化值为null,因此失败

C# 错误:转换为值类型';System.Int32';由于物化值为null,因此失败,c#,asp.net-mvc,entity-framework,linq,C#,Asp.net Mvc,Entity Framework,Linq,我和你有问题 转换为值类型“System.Int32”失败,因为具体化的值为null。结果类型的泛型参数或查询必须使用可为空的类型 我已经找到了解决问题的答案,但没有找到类似的案例 我试图从账户id中输入tblTransactions中的max交易id。如果该帐户没有任何交易记录,则会导致错误。这两个表之间的关系为tblTransaction。accountId=tblAccount.Id 我的控制器: //GET: Balance [Authorize] public Act

我和你有问题

转换为值类型“System.Int32”失败,因为具体化的值为null。结果类型的泛型参数或查询必须使用可为空的类型

我已经找到了解决问题的答案,但没有找到类似的案例

我试图从
账户id
中输入
tblTransactions
中的
max
交易id。如果该帐户没有任何交易记录,则会导致错误。这两个表之间的关系为tblTransaction。accountId=tblAccount.Id

我的控制器:

//GET: Balance

    [Authorize]
    public ActionResult Index()
    {
        string useracc = (string)Session["AccountNumber"];

        var accountInstance = db.Accounts.FirstOrDefault(w => w.AccountNumber.ToString() == useracc);


        List<Transaction> AccountTransactions = db.Transactions.Where(w => w.AccountId == accountInstance.Id&&w.IsCancelled ==false).Select(w => w).OrderByDescending(w => w.Date).ToList();

            var accountStatement = AccountTransactions.Where(w => w.TransactionTypeId == 2 && w.Date.Year >= 2015).OrderByDescending(w => w.Date).ToList();

            var lastTransactionId = db.Transactions.Where(w => w.AccountId == accountInstance.Id && w.IsCancelled == false && w.TransactionTypeId == 2 && w.Date.Year >= 2015).Max(t => t.Id);

            var needDueDate = db.SystemInvoices.Where(s => s.Id == lastTransactionId).Select(s => s.DueDate).FirstOrDefault();



            List<customBalanceInfoItem> currCustomBalance = new List<customBalanceInfoItem>();

            customBalanceInfoItem displayItem = new customBalanceInfoItem();

            displayItem.AccountNumber = accountInstance.AccountNumber;
            displayItem.DueDate = needDueDate;

            currCustomBalance.Add(displayItem);
            return View(currCustomBalance);
        }
//获取:余额
[授权]
公共行动结果索引()
{
字符串useracc=(字符串)会话[“AccountNumber”];
var accountInstance=db.Accounts.FirstOrDefault(w=>w.AccountNumber.ToString()==useracc);
列出AccountTransactions=db.Transactions.Where(w=>w.AccountId==accountInstance.Id&&w.IsCancelled==false)。选择(w=>w).OrderByDescending(w=>w.Date).ToList();
var accountStatement=AccountTransactions.Where(w=>w.TransactionTypeId==2&&w.Date.Year>=2015)。OrderByDescending(w=>w.Date)。ToList();
var lastTransactionId=db.Transactions.Where(w=>w.AccountId==accountInstance.Id&&w.IsCancelled==false&&w.TransactionTypeId==2&&w.Date.Year>=2015)。最大值(t=>t.Id);
var needDueDate=db.SystemInvoices.Where(s=>s.Id==lastTransactionId)。选择(s=>s.DueDate.FirstOrDefault();
List currCustomBalance=新列表();
customBalanceInfoItem displayItem=新建customBalanceInfoItem();
displayItem.AccountNumber=accountInstance.AccountNumber;
displayItem.DueDate=需要的DueDate;
currCustomBalance.Add(显示项);
返回视图(currCustomBalance);
}

var lastTransactionId上发生错误。

请尝试添加第一个或默认值,以防止出现空异常

var lastTransactionId = db.Transactions.Where(w => w.AccountId == accountInstance.Id && w.IsCancelled == false && w.TransactionTypeId == 2 && w.Date.Year >= 2015).Max(t => t.Id).FirstOrDefault();

使用
DefaultIfEmpty()
修改它,选中

修改查询

var lastTransactionId = db.Transactions.Where(w => w.AccountId == accountInstance.Id && w.IsCancelled == false && w.TransactionTypeId == 2 && w.Date.Year >= 2015)
                                       .Select(t=>t.Id)
                                       .DefaultIfEmpty(-1)
                                       .Max()

您可以定义需要返回的值,如果集合为空,就像我将其设置为-1一样,否则它将是默认值

感谢您的解决方案。但是FirstOrDefault()最后似乎无法应用。这在Max本身就是失败的,所以FirstOrDefault不会帮助语法错误
Max()
已返回一个值。因此,
FirstOrDefault()
方法将不存在于您返回的类型上。最后一个
db.Transactions是什么。其中(…)
的计算结果是什么?我正在尝试获取帐户的发票到期日。但是对于一些还没有发票的账户。当我使用这些帐户时,run将导致错误,因为在transactions表中找不到任何accountId。我将列出余额页的完整控制器以及到期日期。只需添加一个检查,查看评估是否为
null
,如果不是,则调用
max
。显示无效帐户的错误消息比仅对其进行阴影处理更合适。我添加的DefaultIfEmpty(-1)可能重复,但仍在DefaultIfEmpty(-1)之前的红线下进行查询。在它之前添加ToList(),这是对列表类型的调用,而您正在对IEnumerable类型执行此操作,请检查编辑的版本Corry,尝试仍不工作,ToList()之前的红线下的查询。DefaultIfEmpty(-1)。Max(t=>t.Id)发布红线提供的确切错误消息,这是一个编译问题验证代码描述项目文件行错误CS1929'List'不包含'DefaultIfEmpty'的定义和最佳扩展方法重载'Queryable.DefaultIfEmpty(IQueryable,int)'需要类型为'IQueryable'CustomerPortal的接收器