Linq Verbosity Problem

Hello Linq Legends.

There must be a way to refactor a method out of the following code as a more elegant and reusable method possibly using Action delegates in linq. (?)

Basically, RmdSessionRequest parameter is an entity object and the IRmdSessionRequestDetails is a dto with nullable int ids which correspond to linked entities in the entity. If either the entity object is null and the dto id isn't, or they are both not null but not equal, i need to update the entity with the dto value.

public void UpdateTriageRequest(RmdSessionRequest request, IRmdSessionRequestDetails requestDetails)
            if ((request.Diagnostician == null && requestDetails.DiagnosticianId.HasValue ) ||
                (request.Diagnostician != null && requestDetails.DiagnosticianId.HasValue && !requestDetails.DiagnosticianId.Value.Equals(request.Diagnostician.Id)))
                var diagnostician = _userRepository.Get(requestDetails.DiagnosticianId.Value);
                request.Diagnostician = diagnostician;

            if ((request.SpecimenIdentification == null && requestDetails.SpecimenIdentificationId.HasValue) ||
                (request.SpecimenIdentification != null && requestDetails.SpecimenIdentificationId.HasValue && !requestDetails.SpecimenIdentificationId.Equals(request.SpecimenIdentification.Id)))
                var specimenIdentification = _specimenIdentificationRepository.Get(requestDetails.SpecimenIdentificationId.Value);
                request.SpecimenIdentification = specimenIdentification;

            if ((request.RmdNode == null && requestDetails.RmdNodeId.HasValue) ||
                (request.RmdNode != null && requestDetails.RmdNodeId.HasValue && !requestDetails.RmdNodeId.Value.Equals(request.RmdNode.Id)))
                var rmdNode = _rmdNodeRepository.Get(requestDetails.RmdNodeId.Value);
                request.RmdNode = rmdNode;

Open in new window

This is a snippet of a much larger method so if  someone can help me craft the above kind of if statement into more beautiful and readable code, I'd be very grateful!!!

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Test the HasValue once at the beginning - if it's false nothing after the && will be executed
The || is a short-circuit, if the null comparison returns true, the right-hand isn't executed, so there's no need for the !null test.
The result of the Get can be assigned to the request in the same line.

Not sure without testing whether you can get away withou the check for null, but this would be worth a try:
            if (requestDetails.DiagnosticianId.HasValue &&  (!requestDetails.DiagnosticianId.Value.Equals(request.Diagnostician.Id)))

if (requestDetails.DiagnosticianId.HasValue && 
               ((request.Diagnostician == null) || (!requestDetails.DiagnosticianId.Value.Equals(request.Diagnostician.Id))))
                request.Diagnostician = _userRepository.Get(requestDetails.DiagnosticianId.Value);

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
crittoAuthor Commented:
Apologies for not closing this sooner.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.