我有一个LINQ查询,根据公司的仓库描述进行分组。现在,我不得不将这个月的项目重量加起来。但是,我有另外一个字段,其中包含废品的总重量,这个字段包含“SCRP”一词。当我这样做时,我得到臭名昭着的对象引用错误未设置为对象错误的实例。我认为这是因为有空值或某些东西。该字段包含其他值或空值。如何求和linq上有一个where子句拉动返回空值的数据的字段?
这是我有的查询。它运行完美,直到我尝试添加UserSequence位:
P.s废品百分比也不起作用,但我认为这是因为相同的问题。
var fistDayofPreviousMonth = DateTime.Today.AddMonths(-4);
var testScrapQuery = from s in mapicsSession.Query<InventoryHistory>()
where s.PostedTimestamp > fistDayofPreviousMonth
orderby s.Warehouse.Description
group s by s.Warehouse.Description
into test
let tw = mapicsSession.Query<InventoryHistory>()
.Where(x => x.Warehouse.Description == test.Key)
.Sum(x => x.Item.Weight)
let sw = mapicsSession.Query<InventoryHistory>()
.Where(x => x.Warehouse.Description == test.Key
&& x.UserSequence == "SCRP")
.Sum(x => x.Item.Weight)
select new
{
Warehouse = test.Key,
TotalWeight = tw,
ScrapWeight = sw
//ScrapPercentage = (sw/tw) * 100
};
当我查询是否实际上存在带有SCRP的字段时,它可以工作,但是当我总结时,它会中断。 –
这是我的完整错误:NHibernate.Exceptions.GenericADOException:无法执行查询[SQL:SQL不可用] ---> System.NullReferenceException:未将对象引用设置为对象的实例。 at lambda_method(Closure,Object []) –