critto
asked on
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.
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!!!
Cheers.
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;
}
}
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!!!
Cheers.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER