Locations and Revenues

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
					
public class Program
{
	public static void Main()
	{
		var data = new List<Location>()
		{
			new Location(){ 
				LocationId = 1,
				LocationName = "First Location",
		        SaleTransactionList = new()
				{
					new() { SaleTransactionId = 1, Amount = 1500, LocationId = 1 },
					new() { SaleTransactionId = 2, Amount = 700, LocationId = 1 },
					new() { SaleTransactionId = 3, Amount = 2400, LocationId = 1 },
				}
			},
			new Location(){ 
				LocationId = 2,
				LocationName = "Second Location",
		        SaleTransactionList = new()
				{
					new() { SaleTransactionId = 1, Amount = 120000, LocationId = 1 },
					new() { SaleTransactionId = 2, Amount = 700.99, LocationId = 1 },
					new() { SaleTransactionId = 3, Amount = 2520.09, LocationId = 1 },
				}
			}
		};

		var q = data.Select(x => new result()
							{
								LocationId = x.LocationId,
								LocationName = x.LocationName,
								Revenues = x.SaleTransactionList.Sum(s => s.Amount)
							}).ToList();
		q.ForEach(x => {
			Console.WriteLine($"Location: {x.LocationName}  |  Revenues: {x.Revenues}");
		});
		
	}
	
	public class Location 
	{
		public int LocationId {get; set;}
		public string LocationName {get; set;}
		public List<SaleTransaction> SaleTransactionList {get; set;}
	}
	
	public class SaleTransaction 
	{
		public int SaleTransactionId {get; set;}
		public double Amount {get; set;}
		public double LocationId  {get; set;}
		
	}
	
	public class result 
	{
		public int LocationId {get; set;}
		public string LocationName {get; set;}
		public double Revenues {get; set;}
	}
							
}
;