Search code examples
entitykey

How to get the value of EntityKey?


How to get the value of EntityKey?

I tried:

String x = Customer.EntityKey.EntityKeyValues[0].Value;

String x = Customer.EntityKey.EntityKeyValues[0].Value.ToString();

String x = Customer.EntityKey.EntityKeyValues;

String x = Customer.EntityKey.EntityKeyValues.ToString();

Ended up with: Object reference not set to an instance of an object.

Please help. Thanks


Solution

  • As you reported that you're getting an Object reference not set to an instance of an object, you might want to check for a null reference;

    String x = (Custormer == null ? null :
                  Customer.EntityKey == null ? null :
                    Customer.EntityKey.EntityKeyValues.Length == 0 ? null :
                      Customer.EntityKey.EntityKeyValues[0].Value);