2

I am building a NET 5 API and am unable to extract and calculate something. I have a table StockTransaction which among other has property Quantity (I skipped some properties for brevity):

public class StockTransaction : BaseEntity
{      
    public int Id { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
    public bool Purchase { get; set; }
    public int Resolved { get; set; }       
}

Suppose I have 7 transactions, all of which are purchase:

List<StockTransaction> list = new List<StockTransaction>
{
new StockTransaction {Id = 1, Purchase = true, Quantity = 10, Resolved = 5, Price = 50},
new StockTransaction {Id = 2, Purchase = true, Quantity = 5, Resolved = 5, Price = 70},
new StockTransaction {Id = 3, Purchase = true, Quantity = 8, Resolved = 8, Price = 25},
new StockTransaction {Id = 4, Purchase = true, Quantity = 7, Resolved = 5, Price = 77},
new StockTransaction {Id = 5, Purchase = true, Quantity = 1, Resolved = 1, Price = 23},
new StockTransaction {Id = 6, Purchase = true, Quantity = 3, Resolved = 0, Price = 14},
new StockTransaction {Id = 7, Purchase = true, Quantity = 2, Resolved = 0, Price = 17},           
};

And I would like to get the value of the last 7 quantities, which in this case gives 176 ((2 x 17) + (3 x 14) + (1 x 23) + (1 x 77)). (How) can this be done? Every help and hint is more then appreciated...

9
  • This should get you started: list .Select(s => s.Quantity * s.Price).Sum(); Commented Jul 15, 2021 at 8:19
  • I assume you know the Select() statement. It's not really clear what keeps you from getting the results you want. Commented Jul 15, 2021 at 8:20
  • Thanks, I know that part, the problem is in extracting the value of only some quantities... Commented Jul 15, 2021 at 8:21
  • ` (3 x 14) + (1 + 23)` why is the third parenthis an addition? Commented Jul 15, 2021 at 8:21
  • I made a mistake while typing... Commented Jul 15, 2021 at 8:24

2 Answers 2

1

You can use

var last3=list.OrderByDescending(x=>x.CreatedDate).Take(3).Select(x=>x.Quantity * x.Price).Sum();

var requiredSum=last3+list.Where(x=>x.id==4).Select(x=>x.Price).FirstOrDefault();
Sign up to request clarification or add additional context in comments.

13 Comments

This is great start, but how to fetch the value of the fourth transaction and take only one quantity with the value of 77?
Ok modifying my answer
So you want values from 4th to 7 transaction only?
yes, but only one quantity from the fourth transaction, and the whole 5, 6, 7 transaction
you can use Skip and Take to acquire sub range of data and ElementAt to get specific records
|
0

Although you have tagged your question with LINQ, do it with a normal loop:

private static decimal SumOfLastTransaction(IEnumerable<StockTransaction> stockTransactions, int max)
    {
        decimal result = 0;
        int sum = 0;
        foreach(var stockTransaction in stockTransactions.OrderByDescending(x => x.Id))
        {
            
            if(sum + stockTransaction.Quantity <= max)
            {
                result += stockTransaction.Quantity * stockTransaction.Price;
                sum += stockTransaction.Quantity;
            }
            else
            {
                result += (max - sum) * stockTransaction.Price;
                return result;
            }
        }
        return result;
    }

You loop over the last items in your list. You sum the amount of transactions and check whether it is smaller than your maximum. As long as they are smaller, you can add the amount of transactions. If not, you check how much is missing until your maximum is reached and substract this from your quantity.

Online demo: https://dotnetfiddle.net/9oM4pj

1 Comment

this is the best answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.