Jagadesh
Jagadesh

Reputation: 374

When no records found throwing Exception in lambda expression

Below is my code i am checking TransmediaTable and filtering.

I manually created a case to satisfy the condition and giving expected results. But when taken new sample the result throwing exception Object reference not set to Instance of Object. Where i am going wrong.

var MissedTransmedias = XMLOperations.TransmediaTable.AsEnumerable()
    .Where(x => x.Field<String>("TO_STRUCTURE_NAME").Contains("CAB:DSLAM") &&
                x.Field<String>("BT_CABLE_TYPE") == "BFT" &&
                string.IsNullOrEmpty(x.Field<String>("TO_EQUIPMENT_NAME")) &&
                ((x.Field<String>("BT_CABLE_DESIGNATION") + x.Field<String>("BT_CABLE_NUMBER")) == OnNumber))
    .Select(x => x.Field<String>("TRANSMEDIA_NAME"));

Upvotes: 0

Views: 299

Answers (2)

Alex
Alex

Reputation: 23300

You can find out what's wrong by splitting up your query like this:

// renamed to keep it short
var data = XMLOperations.TransmediaTable.AsEnumerable();
data = data.Where(x => x.Field<String>("TO_STRUCTURE_NAME").Contains("CAB:DSLAM"))
data = data.Where(x => x.Field<String>("BT_CABLE_TYPE") == "BFT")
data = data.Where(x => string.IsNullOrEmpty(x.Field<String>("TO_EQUIPMENT_NAME")))
data = data.Where(x => ((x.Field<String>("BT_CABLE_DESIGNATION") + x.Field<String>("BT_CABLE_NUMBER")) == OnNumber))
data = data.Select(x => x.Field<String>("TRANSMEDIA_NAME"));

You will be able to find out easily what's going on there, because on debugging you'll be told by the IDE exactly where the exception is originating from.

Upvotes: 1

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

This is potential plase where you can get that exception

x => x.Field<String>("TO_STRUCTURE_NAME").Contains("CAB:DSLAM")

You can fix your code using something like this:

.Where(x => ((x.Field<String>("TO_STRUCTURE_NAME") == null) 
         ? fasle :
           x.Field<String>("TO_STRUCTURE_NAME").Contains("CAB:DSLAM")) && ...

Upvotes: 1

Related Questions