RJ Uy
RJ Uy

Reputation: 397

Updating an item using LINQ, not working

Good day!

I have this query as shown below:

jt.SummarySpecs.Where(
x => true
)
.FirstOrDefault()
.DocSpecs
.Where(
    x => x.DocID == x.DocID
)
.FirstOrDefault()
.FinishingOptionsDesc[0] = option;

But when the code get executed, value for finishingOptionsDesc[0] is not updating...

what's wrong with the query above?


The classes attributes:

"SummarySpecs.cs"
    public DocSpec[] DocSpecs { get; set; }

"DocSpecs.cs"
    public string[] FinishingOptionsDesc { get; set; }

My only concern is to update the FinishingOptionDesc 1st string.

Thanks

Upvotes: 0

Views: 78

Answers (2)

Gopesh Sharma
Gopesh Sharma

Reputation: 7030

Try something like this.. UPDATE

if(jt.SummarySpecs.Select(a=>a.DocSpecs).Any())
{
    var docSpecs = jt.SummarySpecs.Select(a => a.DocSpecs)
    docSpecs.FinishingOptionsDesc[0] = option;
}

Upvotes: 0

OlduwanSteve
OlduwanSteve

Reputation: 1295

That error means something is null. You have 4 unchecked places in one statement where there could be a null return value. Either of the calls to FirstOrDefault could return null if those collections were empty. Or DocSpecs could be null on the returned object, or FinishingOptionsDesc could be null.

Ideally you'd break this statement up a bit and insert null checks. You could say that you know none of these points should ever be null. If that is the case it may be valid to allow an exception to occur, but arguably it is still worth breaking up the statement to get better error reporting on where the exception occurred.

Upvotes: 1

Related Questions