Linq Get totals using group by - c#

I have the following class:
class Item
{
public decimal TransactionValue { get; set; }
public string TransactionType { get; set; }
}
And I have this list:
var items = new List<Item>
{
new Item
{
TransactionValue = 10,
TransactionType = "Income"
},
new Item
{
TransactionValue = 10,
TransactionType = "Income"
},
new Item
{
TransactionValue = -5,
TransactionType = "Outgoing"
},
new Item
{
TransactionValue = -20,
TransactionType = "Outgoing"
}
};
And I am trying to get the sums based on ValueType, I have tried the below but it is adding everything and giving me one total which is -5, what I want is totals for each transaction type so I want to get a new class which is Totals class below and with this data: TotalIncoming : 20 and TotalOutgoing : - 25.
var r = items.Sum(x => x.TransactionValue);
class Totals
{
public decimal TotalIncoming { get; set; }
public decimal TotalOutgoing { get; set; }
}
Thanks

You can achieve your desired result with following query:-
Totals result = new Totals
{
TotalIncoming = items.Where(x => x.TransactionType == "Income")
.Sum(x => x.TransactionValue),
TotalOutgoing = items.Where(x => x.TransactionType == "Outgoing")
.Sum(x => x.TransactionValue)
};
But, as you can see with your Type Totals, we need to hard-code the TransactionType and we have no clue from the result that this Sum belongs to which type apart from the naming convention used.
I will create the below type instead:-
class ItemTotals
{
public string ItemType { get; set; }
public decimal Total { get; set; }
}
Here we will have the TransactionType along with its corresponding Total in the result, we can simply group by TransactionType & calculate the sum, here is the query for same:-
List<ItemTotals> query = items.GroupBy(x => x.TransactionType)
.Select(x => new ItemTotals
{
ItemType = x.Key,
Total = x.Sum(z => z.TransactionValue)
}).ToList();
Here is the Complete Working Fiddle, you can choose from both.

I'm sure there is a probably a clever way to do this in one line using Linq, but everything I could come up with was quite ugly so I went with something a bit more readable.
var results = items.GroupBy(x => x.TransactionType)
.ToDictionary(x => x.Key, x => x.Sum(y => y.TransactionValue));
var totals = new Totals
{
TotalIncoming = results["Income"],
TotalOutgoing = results["Outgoing"]
};

Related

LINQ Group By Year to Form a Tree View

I am trying to create a tree view that would essentially break down like so:
- Year
- Month
- Related Item
So we might have the Year 2022, that has several related items within the several months.
I have created the following model:
public class TreeYear
{
public string NodeYear { get; set; }
public DateTime CreatedDateTime { get; set; }
public List<TreeMonth> Months { get; set; }
}
public class TreeMonth
{
public int MonthID { get; set; }
public string MonthName { get; set; }
public quoteSummary QuoteSummary{ get; set; }
}
I have written some code in my controller which currently returns every item like so:
var allQuotes = QuoteSummary.ToList();
var tree = new TreeYear();
foreach (var quote in allQuotes)
{
tree.NodeYear= quote.CreatedTime.Year.ToString();
tree.CreatedDateTime = quote.CreatedTime;
tree.Months = new List<TreeMonth>()
{
new TreeMonth() {
MonthID = quote.CreatedTime.Month,
MonthName = getAbbreviatedName(quote.CreatedTime.Month),
QuoteSummary = quote
}
};
}
But obviously over here you can see that it has all 41 records of which none are grouped up by year.
I thought maybe I could write some linq something like but at the moment incorrect:
var groups = TheResponse.Details
.GroupBy(
d => Int32.Parse(d.NodeYear),
(key, g) => g.GroupBy(
d => d.Months.Select(x => x.MonthID)),
(key2, g2) => g2.GroupBy(d => d.CreatedDateTime)
)
);
Or would I need to change the model for this idea to work?
If I understood your question correctly, then you need to flatten the inner list and then group by months again.
var groups = TheResponse.Details
.GroupBy(d => Int32.Parse(d.NodeYear))
.Select(d => new
{
Year = d.Key,
MonthObj = d.SelectMany(m => m.Months)
.GroupBy(m => m.MonthID)
.Select(x => new
{
MonthID = x.Key,
RelatedItem = x.ToList()
})
});
I have simplified it by using anonymous types, but you can obviously tweek it based on your resp. Model.

Joining two lists of object optimization

I am looking for a way of optimizing my LINQ query.
Classes:
public class OffersObject
{
public List<SingleFlight> Flights { get; set; }
public List<Offer> Offers { get; set; } = new List<Offer>();
}
public class SingleFlight
{
public int Id { get; set; }
public string CarrierCode { get; set; }
public string FlightNumber { get; set; }
}
public class Offer
{
public int ProfileId { get; set; }
public List<ExtraOffer> ExtraOffers { get; set; } = new List<ExtraOffer>();
}
public class ExtraOffer
{
public List<int> Flights { get; set; }
public string Name { get; set; }
}
Sample object:
var sampleObject = new OffersObject
{
Flights = new List<SingleFlight>
{
new SingleFlight
{
Id = 1,
CarrierCode = "KL",
FlightNumber = "1"
},
new SingleFlight
{
Id = 2,
CarrierCode = "KL",
FlightNumber = "2"
}
},
Offers = new List<Offer>
{
new Offer
{
ProfileId = 41,
ExtraOffers = new List<ExtraOffer>
{
new ExtraOffer
{
Flights = new List<int>{1},
Name = "TEST"
},
new ExtraOffer
{
Flights = new List<int>{2},
Name = "TEST"
},
new ExtraOffer
{
Flights = new List<int>{1,2},
Name = "TEST"
}
}
}
}
};
Goal of LINQ query:
List of:
{ int ProfileId, string CommercialName, List<string> fullFlightNumbers }
FullFlightNumber should by created by "Id association" of a flight. It is created like: {CarrierCode} {FlightNumber}
What I have so far (works correctly, but not the fastest way I guess):
var result = sampleObject.Offers
.SelectMany(x => x.ExtraOffers,
(a, b) => {
return new
{
ProfileId = a.ProfileId,
Name = b.Name,
FullFlightNumbers = b.Flights.Select(f => $"{sampleObject.Flights.FirstOrDefault(fl => fl.Id == f).CarrierCode} {sampleObject.Flights.First(fl => fl.Id == f).FlightNumber}").ToList()
};
})
.ToList();
Final note
The part that looks wrong to me is:
.Select(f => $"{sampleObject.Flights.FirstOrDefault(fl => fl.Id == f)?.CarrierCode} {sampleObject.Flights.FirstOrDefault(fl => fl.Id == f)?.FlightNumber}").ToList()
I am basically looking for a way of "joining" those two lists of the OffersObject by Flight's Id.
Any tips appreciated.
If there will only be a few flights defined in sampleObject.Flights, a sequential search using a numeric key is hard to beat.
However, if the number of flights times the number of offers is substantial (1000s or more), I would suggest loading the list of flights into a dictionary with Id as the key for efficient lookup. Something like:
var flightLookup = sampleObject.Flights.ToDictionary(f => f.Id);
And then calculate your FullFlightNumbers as
FullFlightNumbers = b.Flights
.Select(flightId => {
flightLookup.TryGetValue(flightId, out SingleFlight flight);
return $"{flight?.CarrierCode} {flight?.FlightNumber}";
})
.ToList()
TryGetValue above will quietly return a null value for flight if no match is found. If you know that a match will always be present, the lookup cold alternately be coded as:
SingleFlight flight = flightLookup[flightId];
The above also uses a statement lambda. In short, lambda functions can have either expression or statement blocks as bodies. See the C# reference for more information.
I'd suggest replacing the double .FirstOrDefault() approach with .IntersectBy(). It is available in the System.Linq namespace, starting from .NET 6.
.IntersectBy() basically filters sampleObject.Flights by matching the flight ID for each flight in sampleObject with flight IDs in ExtraOffers.Flights.
In the code below, fl => fl.Id is the key selector for sampleObject.Flights (i.e. fl is a SingleFlight).
var result = sampleObject.Offers
.SelectMany(x => x.ExtraOffers,
(a, b) => {
return new
{
ProfileId = a.ProfileId,
Name = b.Name,
FullFlightNumbers = sampleObject.Flights
.IntersectBy(b.Flights, fl => fl.Id)
.Select(fl => fl.FullFlightNumber) // alternative 1
//.Select(fl => $"{fl.CarrierCode} {fl.FlightNumber}") // alternative 2
.ToList()
};
})
.ToList();
In my suggestion I have added the property FullFlightNumber to SingleFlight so that the Linq statement looks slightly cleaner:
public class SingleFlight
{
public int Id { get; set; }
public string CarrierCode { get; set; }
public string FlightNumber { get; set; }
public string FullFlightNumber => $"{CarrierCode} {FlightNumber}";
}
If defining SingleFlight.FullFlightNumber is not possible/desirable for you, the second alternative in the code suggestion can be used instead.
Example fiddle here.

List<classObject> group by on the objects in C#

I have a requirement to group a list of class objects based on another list inside the Object.
class TransactionObject
{
public int ProjectId { get; set; }
public string uniqueId { get; set; }
public string OrgNumber { get; set; }
public string OrgName { get; set; }
public List<TransactionValue> TransactionValue{ get; set; } = new List<TransactionValue>();
public class TransactionValue
{
public DateTime TrnDate { get; set; }
public decimal EURTrans { get; set; }
public decimal LocaTrans { get; set; }
public decimal BrokeragePercentage { get; set; }
}
}
Now on this class, I have created a list of objects.
var TransactionList = new List<TransactionObject>();
I want to get the list of Unique ProjectsIdm OrgName and The sum of EUR Trans, Local Trans, based on a Group by on TrnDate.
Example:
ProjectId OrgName Trn Date EUR Trns Local Trns
543332 Organization 1 1-Jan-22 100 150
543332 Organization 1 1-Jan-22 150 20
I Need :
Sorry MY BAD I edited the correct output i require
ProjectId OrgName Trn Date EUR Trns Local Trns
543332 Organization 1 1-Jan-22 250 170
What I tried :
List<TransactionObject> result = TransactionList .GroupBy (g => new {
g.HoldingName, g.TransactionValues.First().TrntDate })
.Select(g => g.First())
.ToList();
I tried this, but it doesn't help me with the Sum of the columns, I am good in Java but new to C# please help me out. I have already crossed my deadline on this object.
I think it could be beneficial to split the operation into 2 stages.
var flattened = TransactionList
.SelectMany(
collectionSelector: o => o.Transactions,
resultSelector: (fullObject, transaction) => new { fullObject.ProjectId, fullObject.OrgName, Transaction = transaction });
var grouped = flattened
.GroupBy (t => new {t.ProjectId, t.OrgName, t.Transaction.TrnDate })
.Select( g => new
{
g.Key.ProjectId,
g.Key.OrgName,
g.Key.TrnDate,
SumEURTrans = g.Sum( t => t.Transaction.EURTrans),
SumLocaTrans = g.Sum( t => t.Transaction.LocaTrans)
})
.ToList();
foreach (var t in grouped)
{
Console.WriteLine($"{t.ProjectId}\t{t.OrgName}\t{t.TrnDate}\t{t.SumEURTrans}\t{t.SumLocaTrans}");
}
This produces
543332 Organization 1 1-Jan-22 250 170
543332 Organization 1 2-Jan-22 450 470
543333 Organization 1 1-Jan-22 250 170
for the example input of
var TransactionList = new [] {
new TransactionObject
{
ProjectId = 543332,
OrgName = "Organization 1",
Transactions = new List<TransactionObject.TransactionValue>
{
new TransactionObject.TransactionValue
{
TrnDate = "1-Jan-22",
EURTrans = 100,
LocaTrans = 150
},
new TransactionObject.TransactionValue
{
TrnDate = "1-Jan-22",
EURTrans = 150,
LocaTrans = 20
}
,new TransactionObject.TransactionValue
{
TrnDate = "2-Jan-22",
EURTrans = 200,
LocaTrans = 250
},
new TransactionObject.TransactionValue
{
TrnDate = "2-Jan-22",
EURTrans = 250,
LocaTrans = 220
}
}
},
new TransactionObject
{
ProjectId = 543333,
OrgName = "Organization 1",
Transactions = new List<TransactionObject.TransactionValue>
{
new TransactionObject.TransactionValue
{
TrnDate = "1-Jan-22",
EURTrans = 100,
LocaTrans = 150
},
new TransactionObject.TransactionValue
{
TrnDate = "1-Jan-22",
EURTrans = 150,
LocaTrans = 20
}
}
}
};
This will group by {t.ProjectId, t.OrgName, t.Transaction.TrnDate} across all objects and you need to decide if that's what you want (an example alternative being grouping only within each TransactionObject).
If I understand you right - you can do it this way in one run:
public void GroupTransactions(TransactionObject[] transactionObjects)
{
var results = transactionObjects
// build a flat list of values to have both transactionObject and value for every transactionValue
.SelectMany(obj => obj.TransactionValues.Select(value => (obj, value)))
.GroupBy(tuple => new {tuple.obj.ProjectId, tuple.obj.OrgName, tuple.value.TrnDate})
// Get sums for every group. You can use '.Aggregate()' method instead of this custom GetSum but it seems less readable to me.
.Select(group => (group.Key, sum: GetSum(group.Select(tuple => tuple.value))))
.ToArray();
foreach (var result in results)
Console.WriteLine($"{result.Key.ProjectId} {result.Key.OrgName} {result.Key.TrnDate} {result.sum.euro} {result.sum.local}");
}
private static (decimal euro, decimal local) GetSum(IEnumerable<TransactionObject.TransactionValue> values)
{
decimal euro = 0, local = 0;
foreach (var value in values)
{
euro += value.EURTrans;
local += value.LocaTrans;
}
return (euro, local);
}

Can I put a list of a reference object in the KeySelector in a GroupBy?

Suppose I have a class like this:
public class Transaction
{
public string PointOfSale { get; set; }
public List<PurchaseItem> PurchaseItems { get; set; }
public decimal Cost { get; set; }
}
public class PurchaseItem
{
public ItemType Type { get; set; } //ItemType is an enum
public string Subtype { get; set; }
}
What I want to do is group transactions based on where they were conducted and what items were purchased, and sum the cost.
I feel like GroupBy() would be the solution here, but I don't know how to make it compare PurchaseItems as part of the key, since PurchaseItem is a reference type, and specifying an IEqualityComparer to GroupBy() would have to be able to compare the whole key.
For example:
Transaction[] transactions =
{
new Transaction
{
PointOfSale = "Bytes-R-Us",
PurchaseItems = new List<PurchaseItem>
{
new PurchaseItem { ItemType = ItemType.Electronics, Subtype = "peripherals" },
new PurchaseItem { ItemType = ItemType.Food, Subtype = "candy" }
},
Cost = 50.00
},
new Transaction
{
PointOfSale = "Bytes-R-Us",
PurchaseItems = new List<PurchaseItem>
{
new PurchaseItem { ItemType = ItemType.Electronics, Subtype = "peripherals" },
new PurchaseItem { ItemType = ItemType.Food, Subtype = "candy" }
},
Cost = 25.00
},
new Transaction
{
PointOfSale = "Bytes-R-Us",
PurchaseItems = new List<PurchaseItem>
{
new PurchaseItem { ItemType = ItemType.Software, Subtype = "games" }
},
Cost = 100.00
},
new Transaction
{
PointOfSale = "The Foo Bar & Grill",
PurchaseItems = new List<PurchaseItem>
{
new PurchaseItem { ItemType = ItemType.Food, Subtype = "fine dining" },
new PurchaseItem { ItemType = ItemType.Food, Subtype = "liquor" }
},
Cost = 75.49
}
}
In this case, I would want to see results saying that I spent $75 on peripherals and candy at Bytes-R-Us, $100 on games at Bytes-R-Us, and $75.49 on food and drink at the Foo Bar & Grill. Something like this:
var groupedTransactions = transactions.GroupBy(
x => new {PointOfSale = x.PointOfSale, Items = x.PurchaseItems},
y => y.Cost,
(x, y) => new Transaction
{
PointOfSale = x.PointOfSale,
PurchaseItems = x.Items,
Cost = y.Sum()
});
But how can I make x => new {PointOfSale = x.PointOfSale, Items = x.PurchaseItems} work as I've described?
You can do this in 3 steps basically. Firstly flatten your inner PurchaseItem collections with SelectMany:
var result1 = transactions.SelectMany(
x => x.PurchaseItems,
(y, z) => new
{
y.PointOfSale,
y.Cost,
z.ItemType,
z.Subtype
})
.ToList();
Then use result of that and GroupBy composite key that you needed:
var result2 = result1.GroupBy(x => new { x.PointOfSale, x.ItemType, x.Subtype}).ToList();
Lastly, consolidate your result set with Select:
var result3 = result2.Select(
x => new
{
PointOfSale = x.Key.PointOfSale,
PurchaseItemSubtype = x.FirstOrDefault()?.Subtype,
TotalCost = x.Sum(y => y.Cost)
})
.ToList();
Serialized version of the final result looks like this:
[
{
"PointOfSale":"Bytes-R-Us",
"PurchaseItemSubtype":"peripherals",
"TotalCost":75.0
},
{
"PointOfSale":"Bytes-R-Us",
"PurchaseItemSubtype":"candy",
"TotalCost":75.0
},
{
"PointOfSale":"Bytes-R-Us",
"PurchaseItemSubtype":"games",
"TotalCost":100.0
},
{
"PointOfSale":"The Foo Bar & Grill",
"PurchaseItemSubtype":"fine dining",
"TotalCost":75.49
},
{
"PointOfSale":"The Foo Bar & Grill",
"PurchaseItemSubtype":"liquor",
"TotalCost":75.49
}
]
I used ToList() at the end of each step so that you can step through in debugger and visualize intermediate step results but that is not needed of course. A single linq chain would be:
var result = transactions.SelectMany(
x => x.PurchaseItems,
(y, z) => new
{
y.PointOfSale,
y.Cost,
z.ItemType,
z.Subtype
})
.GroupBy(
x => new
{
x.PointOfSale,
x.ItemType,
x.Subtype
})
.Select(
x => new
{
PointOfSale = x.Key.PointOfSale,
PurchaseItemSubtype = x.FirstOrDefault()?.Subtype,
TotalCost = x.Sum(y => y.Cost)
});
You could build a string from PurchaseItems and use that as part of the key.
For example if you care only about different Subtype combinations:
x => new
{
PointOfSale = x.PointOfSale,
Items = string.Join(", ", x.PurchaseItems.OrderBy(p => p.Subtype).Select(p => p.Subtype))
}

Filtering a List using lambda

I have an object which has properties ID, brandID, brandName, NumPages, and Type.
i need to show the top 5 brands by numPage size, a brand may have multiple IDs, so I need to group by brand
listing.OrderByDescending(o => o.numPage).GroupBy(o=> o.brandName).Take(5).ToList();
is alone the lines of what im looking for but this is not valid code.
It sounds like a given brand name may have several ID's and that you want the top 5 brand's sorted by numPage. Is that correct
If so try the following
var query = listing
.GroupBy(x => x.brandName)
.OrderByDescending(brands => brands.Sum(x => x.numPage))
.Select(x => x.Key)
.Take(5);
Note: After the GroupBy operation you're now passing around a collection of the brand objects instead of single ones. Hence to order by the numPage we need to sum it for all of the brand objects in the group. The .Select(x => x.Key) will select back out the original brandName on which the group is based
just tried and it works:
public class Listing
{
public int ID { get; set; }
public int BrandID { get; set; }
public string BrandName { get; set; }
public int NumPages { get; set; }
public Type Type { get; set; }
}
Here the filtering
Listing listing1 = new Listing() { NumPages = 2, BrandName = "xx" };
Listing listing2 = new Listing() { NumPages = 2, BrandName = "xx" };
Listing listing3 = new Listing() { NumPages = 2, BrandName = "xx" };
Listing listing4 = new Listing() { NumPages = 3, BrandName = "xxxxx" };
List<Listing> allListings = new List<Listing>() { listing1, listing2, listing3, listing4 };
var result = allListings.OrderByDescending(x => x.NumPages).GroupBy(x => x.BrandName).Take(5);

Categories

Resources