3
Answers

How to manipulate a generic sortedlist with object values

peter

peter

1y
732
1

I have SortedList collection. The key is a unique ProductID value whereas the value represents a Product item.

I am trying to find all product objects that have multiple Vendors  (i.e with matching VendorID(UniqueIdentifier) properties). Then carry out some calculations on product properties  e.g discount ShipingFee(decimal) property and the lowest obtained ItemPrice by 10% from each Vendors Product object.

With the exception of those with a ShippingFee equal  to 0.0000 (database value).

The sortedlist should then be updated with the new ShippingFee and ItemPrice values in their original respective  element

Example

vendor1 ,20.39

vendor2 ,10,30

vendor2 ,10,20

output 

vendor2  11,22

Answers (3)
2
Amit Mohanty

Amit Mohanty

17 52.2k 6.1m 1y
public async Task RealtionalCollectionCollectionChange(CancellationToken cancellationToken)
{
    var options = new ChangeStreamOptions
    {
        FullDocument = ChangeStreamFullDocumentOption.UpdateLookup, 
        BatchSize = 2
    };
    var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>().Match("{operationType: { $in: [ 'replace', 'insert', 'update', 'delete' ] } }");
    using(var cursor = await collection.WatchAsync(pipeline, options, cancellationToken))
    {
        while (await cursor.MoveNextAsync(cancellationToken))
        {
            if (cancellationToken.IsCancellationRequested) 
            {
                break;
            }
            int mailCounter = 0; // initialize counter variable
            foreach(var change in cursor.Current)
            {
                if (mailCounter < option.BatchSize) // limit number of emails sent
                {
                    // Sending mail code 
                    mailCounter ++; // increment mail count for this batch
                }
            }
        }
    }
    cursor.Dispose();
}
1
Lalitha Chevuru

Lalitha Chevuru

NA 84 6k 1y

Hi Amit,

It is working for one time i.e., when I delete first batch 5 records it sent 2 mails only, however when I delete second time 5 records, it didn't send any mails as mailcounter is 2.

When I set mailcounter = 0 in else part, first time also it is sending 5 mails.

When I try to dispose cursor it is throwing error.

Not sure how to fix this issue.

Thanks,

Lalitha.C