EF Non-static method requires a target

后端 未结 2 1071
孤城傲影
孤城傲影 2020-12-06 10:52

I\'ve serious problems with the following query.

context.CharacteristicMeasures
        .FirstOrDefault(cm => cm.Charge == null &&
                        


        
相关标签:
2条回答
  • 2020-12-06 10:53

    Try moving newAreaItem == null outside of the query

    bool newAreaItemIsNull = (newAreaItem == null);
    

    and replace newAreaItem == null with newAreaItemIsNull in query.

    Query parser can only operate with the objects in the database, and newAreaItem is not one of them.

    0 讨论(0)
  • 2020-12-06 11:11

    I had the exact same problem as you have when newAreaItem == null is true.

    The problem comes from the fact that the item used in the LINQ cannot be null. Thus, when newAreaItem == null is true it means that newAreaItem is null and this leads to the error being thrown.

    All you can do in my opinion is, after checking newAreaItem == null, to set the newAreaItem to a new empty object of that type if newAreaIteam is null. The newAreaItemIsNull condition will still be in place, thus the

    (cm.AreaItem != null && cm.AreaItem.Id == newAreaItem.Id)
    

    in your code below will still not be evaluated if newAreaItem is null.

    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)));
    
    0 讨论(0)
提交回复
热议问题