我收到错误“无效操作例外”和“需要新表达”
“list”变量。任何人都可以帮助检查我的代码中的错误。
错误在foreach循环上显示null异常。为了进一步的帮助,我附上错误信息图片以便更好地理解。
gomalEntities1 db = new gomalEntities1();
var query = (
from m2 in db.invoices
join m in db.paymentDetails on m2.id equals m.inv_Id
join m3 in db.loadings on m2.fk_loading equals m3.id
join m4 in db.destinations on m2.fk_destination equals m4.id
join m5 in db.vehicle_owner on m2.fk_vehicle equals m5.id
join m6 in db.products on m2.p_Id equals m6.id
orderby m.adv descending
where m2.id == sesstionId select new {
m2.tripDate,
m2.invoiceNo,
m6.productName,
m4.destination1,
m2.freight,
m.inv_Id,
m3.loading1,
m2.shortage_amount,
m2.shortage_Qty,
m5.name,
m5.vehicle,
m5.mobile,
m5.capacity,
amount = (m.adv == true ? m.amount : 0)
});
var list = query.GroupBy(x => x.inv_Id).Select(y => y.FirstOrDefault());
foreach (var r in list)
{
data.Add(new reportClass {
tripDate = r.tripDate != null ? r.tripDate.Value.ToString("D"): "",
InvoiceNo = r.invoiceNo != null ? r.invoiceNo.ToString(): "",
ProductNo = r.productName != null ? r.productName.ToString(): "",
Destination = r.destination1 != null ? r.destination1.ToString(): "",
Freight = r.freight != null ? r.freight.ToString(): "",
Loading = r.loading1 != null ? r.loading1.ToString(): "",
ShortAmount = r.shortage_amount != null ? r.shortage_amount.ToString(): "",
ShortQty = r.shortage_Qty != null ? r.shortage_Qty.ToString(): "",
TripAdvance = r.amount != null ? r.amount.ToString(): ""
});
totalFreight += r.freight;
t_shortageAmount += r.shortage_amount;
t_tripAdv += r.amount;
}