0

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
tshoemake
  • 1,311
  • 1
  • 17
  • 28

1 Answers1

1

Write a little method,

eg

boolean compareDTOAndListItem(DTOAddNewSpecialNeed dto, AddEditSpecialNeedList item) {
    //comparison logic here
    if (dto.SpecialNeedTypeID != item.SpecialNeedTypeId)
        return false;

    return true;
}

Then you can easily use this method anywhere, including in LinQ queries. You could create an extra method to compare 2 lists, or use advanced Linq queries (Join,...) to compare lists

example, find items that matches a dto

var dto = yourdto;
var items = yourlistofitems;
var matchingitems = items.Select(x => compareDTOAndListItem(dto, x));

Hope it helps..

Edit : use it to compare lists

can't you use the any Linq comparer ?

var listitems = allyourlistitems;
var dtos => allyourdtos;

var dtomatches = dtos.Where(d =>listitems.Any(l => compareDTOAndListItem(d, x));

var dotnotmatching = dtos.where(d => !dtomatches.Contains(d));

untested query typed late in the evening... but you get the idea

Luc Debliquis
  • 304
  • 1
  • 6
  • While that is helpful, thats what my logic code is in the op. It's within a small function called 'CompareDtoToDb'. This is the comparison logic that i need to know how to properly work. – tshoemake Dec 06 '16 at 19:07
  • I updated my op to hopefully help – tshoemake Dec 06 '16 at 19:11
  • I don't get you fully there. The comparison logic is specific to your application & business rules. Imho, the comparison method should simply return false at the first field comparison failure , or true at the end. so you can filter lists based on that operator method – Luc Debliquis Dec 06 '16 at 19:20
  • I'm basically modifying a list to return to either insert or update to the db. But first I need to get a list that match, and a list that do not match. Evaluate further with more business logic specific to my application, then pass the list back. – tshoemake Dec 06 '16 at 19:24
  • can't you use the any Linq comparer ? var listitems = allyourlistitems; var dtos => allyourdtos; var dtomatches = dtos.Where(d =>listitems.Any(l => compareDTOAndListItem(d, x)); var dotnotmatching = dtos.where(d => !dtomatches.Contains(d)); untested query typed late in the evening... but you get the idea – Luc Debliquis Dec 06 '16 at 20:07