How can I find a profitable trader from a list of trades using LINQ?

Marius A 31 Reputation points
2021-10-23T12:50:17.067+00:00

I have list of trades with the following type:

public class Trade {
    public string Sender { get; set; }
    public string Receiver { get; set; }
    public string ItemId { get; set; }
    public int Price { get; set; }
}

List<Trade> trades;

Using that list I want to find the most profitable trader.

So I have to find all trades where someone bought an item for a price and then sold it for a higher price. What LINQ query can I use to achieve this?

So far I have tried finding all sellers and buyers and then merge both lists. But I wasn't able to figure out how to combine both.

Developer technologies | C#
{count} vote

1 answer

Sort by: Most helpful
  1. P a u l 10,761 Reputation points
    2021-10-23T13:59:38.02+00:00

    This will give a breakdown of profits for traders of particular items:

    List<Trade> trades = new() {
        new Trade { ItemId = "Item 1", Sender = "Person 1", Receiver = "Person 2", Price = 100 },
        new Trade { ItemId = "Item 1", Sender = "Person 2", Receiver = "Person 3", Price = 110 },
        new Trade { ItemId = "Item 1", Sender = "Person 3", Receiver = "Person 2", Price = 120 },
        new Trade { ItemId = "Item 2", Sender = "Person 4", Receiver = "Person 2", Price = 500 }
    };
    
    var traderDetails = trades
        .GroupBy(t => new { t.Receiver, t.ItemId })
        .Select(t => {
            var totalBuyPrice = t.Sum(_ => _.Price);
            var totalSellPrice = trades.Where(_ => _.Sender == t.Key.Receiver && _.ItemId == t.Key.ItemId).Sum(_ => _.Price);
    
            return new {
                Trader = t.Key.Receiver,
                t.Key.ItemId,
                TotalBuyPrice = totalBuyPrice,
                TotalSellPrice = totalSellPrice,
                Profit = totalSellPrice - totalBuyPrice,
                Profitable = totalSellPrice - totalBuyPrice > 0
            };
        });
    
    foreach (var details in traderDetails) {
        if (details.Profitable) {
            ForegroundColor = ConsoleColor.Green;
        } else {
            ForegroundColor = ConsoleColor.Red;
        }
    
        WriteLine(details.Trader + " bought " + details.ItemId + " for a total of " + details.TotalBuyPrice + " and sold for a total of " + details.TotalSellPrice + " Profit: " + details.Profit);
    }
    

    Or if you the most profitable trader overall:

    var mostProfitable = traderDetails.GroupBy(t => t.Trader)
        .OrderByDescending(t => t.Sum(_ => _.Profit)).Take(1).Select(_ => _.Key).FirstOrDefault();
    
    WriteLine();
    WriteLine("Most profitable: " + mostProfitable);
    
    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.