C# linq int类型为null异常

异常堆栈

fail: Microsoft.EntityFrameworkCore.Query[10100]
      An exception occurred while iterating over the results of a query for context type 'SdVipServer.SdModels.SdDBContext'.
      System.InvalidOperationException: Nullable object must have a value.
         at lambda_method879(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator)
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
      System.InvalidOperationException: Nullable object must have a value.
         at lambda_method879(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator)
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
fail: SdVipServer.Program[0]
      An error occurred creating the DB.
      System.InvalidOperationException: Nullable object must have a value.
         at lambda_method879(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator)
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
         at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
         at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

报错的LINQ代码

from t in dbContext.LTraders
                           join p in dbContext.DShops on t.Shopid equals p.Shopid into result1
                           from row1 in result1.DefaultIfEmpty()
                           join d in dbContext.DsDistributors on t.Traderid equals d.Clientid into result2
                           from row2 in result2.DefaultIfEmpty()
                           where (t.Traderid > 0)   && (row2.Status == null || row2.Status == 1) 
                           orderby t.Code
                           select new {
                               t.Traderid, t.Shipto, t.Scamt, t.Code, t.Name, t.Areaid,
                               t.Countryid, t.Isvendor, t.Isclient, t.Isother, t.Credit, t.Creditday,
                               t.Closed, t.Suramt, t.Speedsymbol, t.Iscashtrader, t.Shopid, t.Acctrader,
                               t.Paymethodid, t.TaxRate,
                               Shopname = row1.Name ?? "",
                               t.Moneyid, t.Empid, t.Departmentid,
                               t.Isexpress, Distributorid =(row2.Distributorid == null ? 0 : row2.Distributorid), row2.Kind ,
                               t.Contactor
                           };

row2.Kind 的类型信息是:
public int Kind { get; set; }

因为数据库是空的
row2.Kind的值是null
改成 Kind = row2.Kind == null ? 0 : row2.Kind
或者 public int? Kind { get; set; }
都可以解决此异常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值