Reputation: 4198
Greetings I've got a logic question I have got 2 procedures
First is linq that looks like:
_sharedDocumentsAttachments = SourceDocumentAttachmentMeta
.Where(sDoc => TargetDocumentAttachmentMeta.Any(tDoc => tDoc.DocumentBridgeId == sDoc.DocumentId)).ToList();
Where
List<DocumentAttachment> _sharedDocumentsAttachments;
And
SharedDocumnentAttachmentConnector = new Dictionary<int, int>();
foreach (DocumentAttachment document in _sharedDocumentsAttachments)
{
foreach (DocumentAttachment tDoc in TargetDocumentAttachmentMeta.Where(tDoc => document.DocumentId == tDoc.DocumentBridgeId))
{
SharedDocumnentAttachmentConnector.Add(document.DocumentId, tDoc.DocumentId);
}
}
And I was wondering if I can attach second procedure to the first one somehow, as basically the are doing same compare but adding values to 2 different collections?
I was experimenting with for each but it wont work correctly.
Upvotes: 2
Views: 72
Reputation: 37299
You can do this: join the two collections and then specify how to convert to dictionary
_sharedDocumentsAttachments.Join(TargetDocumentAttachmentMeta,
document => document.DocumentId,
tDoc => tDoc.targetDocument,
(document, tDoc) => new
{
Key = document.DocumentId,
Value = tDoc.DocumentId
})
.ToDictionary(item => item.Key,
item => item.Value);
Take note that if the join will result with 2 "records" for the same document.DocumentId it will throw a ArgumentException: An item with the same key has already been added
.
If that might happen then think of:
LookUp
Dictionary<int,IEnumerable<int>>
For differences between the two check this
Upvotes: 2