All of the class here share these same property
Country, Total and Date.
Total property are integer in some classes and decimal in some classed.
The extension method below has a lot of duplication. i have tried to refactor it using interface but i can't find a way to refactor it at the last step on selection since it need different class
public static class TotalApprovedPOAmountStatisticExtension
{
public static void MergeNullAndEmplyCountry(this IEnumerable<TotalApprovedPOAmountStatistic> totalUserStatistic)
{
foreach (var item in totalUserStatistic)
{
if (string.IsNullOrWhiteSpace(item.Country))
{
item.Country = "";
}
}
totalUserStatistic = totalUserStatistic
.GroupBy(item => new { item.Country, item.Date })
.Select(groupedUser => new TotalApprovedPOAmountStatistic()
{ Country = groupedUser.Key.Country, Total = groupedUser.Sum(item => item.Total), Date = groupedUser.Key.Date }).ToList();
}
}
public static class TotalPOStatisticListExtension
{
public static void MergeNullAndEmplyCountry(this IEnumerable<TotalPOStatistic> totalUserStatistic)
{
foreach (var item in totalUserStatistic)
{
if (string.IsNullOrWhiteSpace(item.Country))
{
item.Country = "";
}
}
totalUserStatistic = totalUserStatistic
.GroupBy(item => new { item.Country, item.Date })
.Select(groupedUser => new TotalPOStatistic() { Country = groupedUser.Key.Country, Total = groupedUser.Sum(item => item.Total), Date = groupedUser.Key.Date }).ToList();
}
}
public static class TotalProjectAmountStatisticListExtension
{
public static void MergeNullAndEmplyCountry(this IEnumerable<TotalProjectAmountStatistic> totalUserStatistic)
{
foreach (var item in totalUserStatistic)
{
if (string.IsNullOrWhiteSpace(item.Country))
{
item.Country = "";
}
}
totalUserStatistic = totalUserStatistic
.GroupBy(item => new { item.Country, item.Date })
.Select(groupedUser => new TotalProjectAmountStatistic() { Country = groupedUser.Key.Country, Total = groupedUser.Sum(item => item.Total), Date = groupedUser.Key.Date }).ToList();
}
}
public static class TotalProjectStatisticListExtension
{
public static void MergeNullAndEmplyCountry(this IEnumerable<TotalProjectStatistic> totalUserStatistic)
{
foreach (var item in totalUserStatistic)
{
if (string.IsNullOrWhiteSpace(item.Country))
{
item.Country = "";
}
}
totalUserStatistic = totalUserStatistic
.GroupBy(item => new { item.Country, item.Date })
.Select(groupedUser => new TotalProjectStatistic() { Country = groupedUser.Key.Country, Total = groupedUser.Sum(item => item.Total), Date = groupedUser.Key.Date }).ToList();
}
}
public static class TotalUserStatisticListExtension
{
public static void MergeNullAndEmplyCountry(this IEnumerable<TotalUserStatistic> totalUserStatistic)
{
foreach (var item in totalUserStatistic)
{
if (string.IsNullOrWhiteSpace(item.Country))
{
item.Country = "";
}
}
totalUserStatistic = totalUserStatistic
.GroupBy(item => new { item.Country, item.Date })
.Select(groupedUser => new TotalUserStatistic() { Country = groupedUser.Key.Country, Total = groupedUser.Sum(item => item.Total), Date = groupedUser.Key.Date }).ToList();
}
}