15

I've serious problems with the following query.

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)));

I get a TargetException: Non-static method requires a target when newAreaItem is null. If newAreaItem is not null I get an NotSupportedException: Unable to create a constant value of type 'PQS.Model.AreaItem'. Only primitive types or enumeration types are supported in this context.

Things I've already checked if they're null: c, newLine, actShiftIndex all 3 variables are not null and the Id is accessible.

I dont get it... please help.

If u need more information.. dont hesitate to ask...

UPDATE

I could eliminate the NotSupportedException, but I still got the TargetException when my newAreaItemIsNull is true.. :/

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)));

UPDATE

I finally did it. It seems that the query parse can't parse my newAreaItem(IsNull) because it's not in the DB model somehow !? I have to split my queries..

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);

Does someone know a better solution?

Staeff
  • 4,994
  • 6
  • 34
  • 58
JuHwon
  • 2,033
  • 2
  • 34
  • 54
  • possible duplicate of [Non-static method requires a target. Entity Framework 5 Code First](http://stackoverflow.com/questions/13210867/non-static-method-requires-a-target-entity-framework-5-code-first) – Chris Moschini Feb 09 '15 at 14:15

2 Answers2

18

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.

alex
  • 12,464
  • 3
  • 46
  • 67
  • 3
    thanks. this helped to eliminate the NotSupportedException. Still when the newAreaitem is null i get a `TargetException: Non-static method requires a target` :/ – JuHwon Mar 20 '13 at 09:35
  • 1
    i did fix it!! your answer was the key. it seems that the query parser cant even operate with my boolean flag if it is true.. it seems that i have to write 2 times a nearly equal query. :/ you know a better solution ? – JuHwon Mar 20 '13 at 09:44
0

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)));
CristisS
  • 1,103
  • 1
  • 12
  • 31