EF非静态方法需要目标

JuH*_*won 14 c# linq entity-framework

我对以下查询存在严重问题.

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)));
Run Code Online (Sandbox Code Playgroud)

TargetException: Non-static method requires a target 当newAreaItem为null时,我得到一个.如果newAreaItem不为null,我得到一个NotSupportedException: Unable to create a constant value of type 'PQS.Model.AreaItem'. Only primitive types or enumeration types are supported in this context.

我已经检查过它们是否为null:c,newLine,actShiftIndex所有3个变量都不为空且可以访问Id.

我不明白......请帮忙.

如果你需要更多信息..不要犹豫,问...

UPDATE

我可以消除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)));
Run Code Online (Sandbox Code Playgroud)

UPDATE

我终于做到了.似乎查询解析无法解析我,newAreaItem(IsNull)因为它不是以某种方式在数据库模型中!我必须分开我的查询..

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);
Run Code Online (Sandbox Code Playgroud)

有人知道更好的解决方案吗?

ale*_*lex 17

尝试移出newAreaItem == null查询

bool newAreaItemIsNull = (newAreaItem == null);
Run Code Online (Sandbox Code Playgroud)

并更换newAreaItem == nullnewAreaItemIsNull查询.

查询解析器只能与数据库中的对象一起操作,而newAreaItem不是其中之一.

  • 谢谢.这有助于消除NotSupportedException.仍然当newAreaitem为null时,我得到一个`TargetException:非静态方法需要一个目标`:/ (3认同)