Refactor Linq grouping 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&lt;TotalApprovedPOAmountStatistic&gt; totalUserStatistic)
    {
        foreach (var item in totalUserStatistic)
        {
            if (string.IsNullOrWhiteSpace(item.Country))
            {
                item.Country = "";
            }
        }

        totalUserStatistic = totalUserStatistic
                            .GroupBy(item =&gt; new { item.Country, item.Date })
                            .Select(groupedUser =&gt; new TotalApprovedPOAmountStatistic() 
                            { Country = groupedUser.Key.Country, Total = groupedUser.Sum(item =&gt; item.Total), Date = groupedUser.Key.Date }).ToList();

    }
}

    public static class TotalPOStatisticListExtension
{
    public static void MergeNullAndEmplyCountry(this IEnumerable&lt;TotalPOStatistic&gt; totalUserStatistic)
    {
        foreach (var item in totalUserStatistic)
        {
            if (string.IsNullOrWhiteSpace(item.Country))
            {
                item.Country = "";
            }
        }

        totalUserStatistic = totalUserStatistic
                            .GroupBy(item =&gt; new { item.Country, item.Date })
                            .Select(groupedUser =&gt; new TotalPOStatistic() { Country = groupedUser.Key.Country, Total = groupedUser.Sum(item =&gt; item.Total), Date = groupedUser.Key.Date }).ToList();

    }
}

    public static class TotalProjectAmountStatisticListExtension
{
    public static void MergeNullAndEmplyCountry(this IEnumerable&lt;TotalProjectAmountStatistic&gt; totalUserStatistic)
    {
        foreach (var item in totalUserStatistic)
        {
            if (string.IsNullOrWhiteSpace(item.Country))
            {
                item.Country = "";
            }
        }

        totalUserStatistic = totalUserStatistic
                            .GroupBy(item =&gt; new { item.Country, item.Date })
                            .Select(groupedUser =&gt; new TotalProjectAmountStatistic() { Country = groupedUser.Key.Country, Total = groupedUser.Sum(item =&gt; item.Total), Date = groupedUser.Key.Date }).ToList();

    }
}

    public static class TotalProjectStatisticListExtension
{
    public static void MergeNullAndEmplyCountry(this IEnumerable&lt;TotalProjectStatistic&gt; totalUserStatistic)
    {
        foreach (var item in totalUserStatistic)
        {
            if (string.IsNullOrWhiteSpace(item.Country))
            {
                item.Country = "";
            }
        }

        totalUserStatistic = totalUserStatistic
                            .GroupBy(item =&gt; new { item.Country, item.Date })
                            .Select(groupedUser =&gt; new TotalProjectStatistic() { Country = groupedUser.Key.Country, Total = groupedUser.Sum(item =&gt; item.Total), Date = groupedUser.Key.Date }).ToList();

    }
}

    public static class TotalUserStatisticListExtension
{
    public static void MergeNullAndEmplyCountry(this IEnumerable&lt;TotalUserStatistic&gt; totalUserStatistic)
    {
        foreach (var item in totalUserStatistic)
        {
            if (string.IsNullOrWhiteSpace(item.Country))
            {
                item.Country = "";
            }
        }

        totalUserStatistic = totalUserStatistic
                            .GroupBy(item =&gt; new { item.Country, item.Date })
                            .Select(groupedUser =&gt; new TotalUserStatistic() { Country = groupedUser.Key.Country, Total = groupedUser.Sum(item =&gt; item.Total), Date = groupedUser.Key.Date }).ToList();

    }
}
You can use an interface and a generic method to remove the code duplication:

public interface IStatistic
{
    string Country {get; set;}
    DateTime Date {get; set;}
    Decimal Total {get; set;}
}

public static class StatisticExtension
{
    public static void MergeNullAndEmplyCountry&lt;T&gt;(this IEnumerable&lt;T&gt; totalUserStatistic) where T : IStatistic, new()
    {
        foreach (var item in totalUserStatistic)
            if (string.IsNullOrWhiteSpace(item.Country))
                item.Country = "";

        totalUserStatistic = totalUserStatistic.GroupBy(item =&gt; new { item.Country, item.Date })
                                                .Select(groupedUser =&gt; new T() 
                                                { 
                                                    Country = groupedUser.Key.Country, 
                                                    Total = groupedUser.Sum(item =&gt; item.Total), 
                                                    Date = groupedUser.Key.Date 
                                                }).ToList();
    }
}


Important here is the new() constraint, which allows us to create an instance of T.

If the Total property of a class is int instead of decimal, we can make use of an explicit interface implementation:

public class BlaStatistic : IStatistic
{
    public string Country {get; set;}
    public DateTime Date {get; set;}
    public Int32 Total {get; set;}
    Decimal IStatistic.Total 
    {   
        get { return Convert.ToDecimal(Total); }
        set { Total = Convert.ToInt32(value); }
    }
}


Also, your extension method should actually return something, because it won't change the IEnumerable&lt;...&gt; you pass in:

public static IEnumerable&lt;T&gt; MergeNullAndEmplyCountry&lt;T&gt;(this IEnumerable&lt;T&gt; totalUserStatistic) where T : IStatistic, new()
{
    ...
    return totalUserStatistic.GroupBy...
}