Question

(See this question for a little background on this one.)

I need to match up items in a List<T> collection of Entity records with a List<T> collection of objects (the Entity object collection is of type Citation and the other is of type sRecord). There is a 1-to-1 relationship of sorts between the second collection and the first where each of the objects in the second match up to exactly one record in the first (but not necessarily vice versa). They match on a single field called ID in the Citation class and id in the sRecord class. Trying to run through nested loops to match them up quickly became bogged down, so I sought out a means to match up the entire collections once and then iterate through that matched set.

This is how I put together the suggested group join statement:

var draftMatches = draftRecords.GroupJoin(sRecords,
    Citation => Citation.ID,
    stiRecord => sRecord.id,
    (Citations, sRecords) =>
    new
    {
        Citation = Citations,
        sRecord = sRecords.Select(sRecord => sRecord)
    });

I don't have much confidence that I got it right.

What I need to do with the resulting matched set is compare fields in the sRecord object to fields in the Citation object to determine whether the Citation object needs to be updated. Since I'm working with Entity, I assumed that I needed to preserve the reference to the Citation object when I updated it in a separate method (separated for code reuse purposes), so this is how I'm trying to do that update:

DateTime recordDate = RecordUtilities.ConvertToDate(match.sRecord.FirstOrDefault().modifiedDate);
int comparison = recordDate.CompareTo((DateTime)match.Citation.modifiedDate);

if (comparison > 0)
{
    EntityUtilities.MapToCitation(ref match.Citation, match.sRecord.FirstOrDefault());
    updatedDraft++;
}

At this point, I'm getting an IntelliSense error on match.Citation in the call to MapToCitation, stating "A property, indexer, or dynamic member access may not be passed as an out or ref parameter."

I need to ensure that the Citation object that gets saved when I do context.Save() is updated from the sRecord object if appropriate. I also need to make sure that I'm saving time with the matching over the previous code. What do I need to change about my code to achieve these goals?

Was it helpful?

Solution

var draftRecordDic = draftRecords.ToDictionary(record => record.ID);
var sRecordsDic = sRecords.ToDictionary(record => record.ID);

var validKeys = sRecordsDic.Keys.Intersect(draftRecordDic.Keys);
foreach(var key in validKeys)
{
   var recOriginal = draftRecordDic[key];
   var recMo = sRecordsDic[key];

   // do your code here.
}

This should work nicely & is simple to understand.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top