I have a DTO class and a table thats converted to a list using Entity Framework.
When my page posts, it sends a list of my dto. What I'd like to do is write code to compare the dto list with the EF list based on specific elements. This is my code...
MY dto
public class DTOAddNewSpecialNeed
{
public long PersonId { get; set; }
public List<AddEditSpecialNeedList> SelectedSpecialNeeds { get; set; }
public List<AddEditSpecialNeedList> AvailableSpecialNeeds { get; set; }
public Guid LtcConcurrencyCheck { get; set; }
public class AddEditSpecialNeedList
{
public int PersonSpecialNeedId { get; set; }
public int SpecialNeedTypeId { get; set; }
public string SpecialNeedDescription { get; set; }
public DateTime? StopDate { get; set; }
public int SortOrder { get; set; }
}
}
My code to access... dtoSelectedList is my list of dto's passed in... my dbSelectedList is my ef object (a table) I'm comparing to. Essentially I want to keep a list of all dto's that exist in the db (dbselectelist where the specialneedtypeid's match and specialneedtype isnt other(-12). I also can have duplicates. Right now this code doesnt account for it. Is there a better way to write this code or am i just doing my comparisons wrong?
var matchedDtos = dtoSelectedList.SelectMany(dto => dbSelectedList
.Where(db => db.SpecialNeedTypeID == dto.SpecialNeedTypeId
&& dto.SpecialNeedTypeId != -12)).ToList();
var matchedOtherDtos = dtoSelectedList.SelectMany(dto => dbSelectedList
.Where(db => db.SpecialNeedTypeID == dto.SpecialNeedTypeId
&& dto.SpecialNeedTypeId == (int)SpecialNeedType.Other
&& db.OtherTypeText == dto.SpecialNeedDescription.Replace("Other: ", "").Trim())).ToList();
My method signature where the above logic is. The SpecialNeed class is the class generated by entity:
public List<SpecialNeed> NeedsCompareDtoToDb(List<DTOAddNewSpecialNeed.AddEditSpecialNeedList> dtoSelectedList, List<SpecialNeed> dbSelectedList