C# EF非静态方法需要一个目标
我对下面的查询有严重的问题C# EF非静态方法需要一个目标,c#,linq,entity-framework,C#,Linq,Entity Framework,我对下面的查询有严重的问题 context.CharacteristicMeasures .FirstOrDefault(cm => cm.Charge == null && cm.Characteristic != null && cm.Characteristic.Id == c.Id &&
context.CharacteristicMeasures
.FirstOrDefault(cm => cm.Charge == null &&
cm.Characteristic != null &&
cm.Characteristic.Id == c.Id &&
cm.Line != null &&
cm.Line.Id == newLine.Id &&
cm.ShiftIndex != null &&
cm.ShiftIndex.Id == actShiftIndex.Id &&
(newAreaItem == null ||
(cm.AreaItem != null &&
cm.AreaItem.Id == newAreaItem.Id)));
我得到了一个TargetException:当newAreaItem为null时,非静态方法需要一个target
。
如果newAreaItem不为null,我将得到一个NotSupportedException:无法创建“PQS.Model.AreaItem”类型的常量值。在此上下文中仅支持基元类型或枚举类型。
我已经检查过的内容是否为空:
c、 换行符、actShiftIndex所有3个变量均不为空,且Id可访问
我不明白。。。请帮忙
如果你需要更多的信息。。别犹豫,问问
更新
我可以消除NotSupportedException
,但当我的newAreaItemIsNull为true时,我仍然得到TargetException..:/
bool newAreaItemIsNull = (newAreaItem == null);
var mc = context.CharacteristicMeasures
.FirstOrDefault(cm => cm.Charge == null &&
cm.Characteristic != null &&
cm.Characteristic.Id == c.Id &&
cm.Line != null &&
cm.Line.Id == newLine.Id &&
cm.ShiftIndex != null &&
cm.ShiftIndex.Id == actShiftIndex.Id &&
(newAreaItemIsNull ||
(cm.AreaItem != null &&
cm.AreaItem.Id == newAreaItem.Id)));
更新
我终于做到了。查询解析似乎无法解析mynewAreaItem(IsNull)
,因为它不知何故不在DB模型中!?
我得把我的问题分开
bool newAreaItemIsNull = (newAreaItem == null);
MeasureCharacteristic mc;
if (newAreaItemIsNull)
mc = context.CharacteristicMeasures
.FirstOrDefault(cm => cm.Charge == null &&
cm.Characteristic != null &&
cm.Characteristic.Id == c.Id &&
cm.Line != null &&
cm.Line.Id == newLine.Id &&
cm.ShiftIndex != null &&
cm.ShiftIndex.Id == actShiftIndex.Id);
else
mc = context.CharacteristicMeasures
.FirstOrDefault(cm => cm.Charge == null &&
cm.Characteristic != null &&
cm.Characteristic.Id == c.Id &&
cm.Line != null &&
cm.Line.Id == newLine.Id &&
cm.ShiftIndex != null &&
cm.ShiftIndex.Id == actShiftIndex.Id &&
cm.AreaItem != null &&
cm.AreaItem.Id == newAreaItem.Id);
有人知道更好的解决方案吗?尝试将
newAreaItem==null
移动到查询之外
bool newAreaItemIsNull = (newAreaItem == null);
并将查询中的newAreaItem==null
替换为newAreaItemIsNull
查询解析器只能对数据库中的对象进行操作,而newAreaItem不是其中之一。当
newAreaItem==null
为true时,我遇到了与您完全相同的问题
问题来自这样一个事实:LINQ中使用的项不能为null。因此,当newAreaItem==null
为true时,意味着newAreaItem
为null,这将导致抛出错误
我认为,在选中newAreaItem==null
之后,如果newAreaIteam
为null,则可以将newAreaItem设置为该类型的新空对象。newAreaItemIsNull
条件仍将存在,因此
(cm.AreaItem != null && cm.AreaItem.Id == newAreaItem.Id)
如果newAreaItem
为空,则仍不会对下面的代码进行计算
context.CharacteristicMeasures.
FirstOrDefault(cm => cm.Charge == null &&
cm.Characteristic != null && cm.Characteristic.Id == c.Id &&
cm.Line != null && cm.Line.Id == newLine.Id &&
cm.ShiftIndex != null && cm.ShiftIndex.Id == actShiftIndex.Id &&
(newAreaItem == null ||
(cm.AreaItem != null && cm.AreaItem.Id == newAreaItem.Id)));
谢谢这有助于消除NotSupportedException。当newAreaitem为null时,我仍然会得到一个
TargetException:Non-static方法需要一个目标:/i确实修复了它!!你的答案是关键。如果我的布尔标志为真,那么查询解析器似乎甚至无法使用它进行操作。。似乎我必须写两次几乎相等的查询。:/你知道更好的解决方案吗?可能是