日期:2014-05-20  浏览次数:21200 次

linq中左连接查询,报错“值不能为空,参数名:row”该如何解决
var queryByService = from GStocks in dtGoodsStocks.AsEnumerable()
  join SalesCount in dtt.AsEnumerable()
  on GStocks.Field<String>("GoodsNo")
  equals SalesCount.Field<String>("GoodsNo") into mm
  from SalesCount in mm.AsEnumerable().DefaultIfEmpty()
  where (GStocks.Field<int>("StocksAll") >= 1) || (SalesCount.Field<int>("Out") > 0) select new
  {
  商品编号 = GStocks.Field<string>("GoodsNo"),//.Split('-')[2],
  总库存 = GStocks.Field<int>("StocksAll"),
  可销售数量 = GStocks.Field<int>("available"),
  出库数量 = SalesCount.Field<int>("Out")==Convert.ToInt32(DBNull.Value) ? 0 : SalesCount.Field<int>("Out") };

以上是我的语句dtGoodsStocks、SalesCount 是两个数据源,其中SalesCount 中数据为空,在查询的时候(SalesCount.Field<int>("Out") > 0)以及出库数量 = SalesCount.Field<int>("Out")==Convert.ToInt32(DBNull.Value) ? 0 : SalesCount.Field<int>("Out")这两个位置都报错值不能为空,我该如何修改呢?
雪地跪求大侠相助

------解决方案--------------------
你可以搜关键词 C# linq left join null
给你个例子,
C# code
var query = (from keyword in context.Keywords
     join ignore in context.IgnoreWords 
        on keyword.WordID equals ignore.ID into ignored
     from i in ignored.DefaultIfEmpty()
     where i == null
     where keyword.DomainID == ID
     orderby keyword.Score descending
     select keyword).Take(10);

------解决方案--------------------
where (GStocks.Field<int>("StocksAll") >= 1) || (SalesCount.Field<int>("Out") > 0) select new
===========
where (GStocks.Field<int>("StocksAll") >= 1) || (SalesCount!=null && SalesCount.Field<int>("Out") > 0) select new



出库数量 = SalesCount.Field<int>("Out")==Convert.ToInt32(DBNull.Value) ? 0 : SalesCount.Field<int>("Out") };
==========
出库数量 = SalesCount==null ? 0 : SalesCount.Field<int>("Out") };