Home > database >  Select 2 records with more current months C# .NET
Select 2 records with more current months C# .NET

Time:03-31

I am trying to make a query to "paymentEvolutionDetail.EvolutionPaymentSystemsByMonth" where a new data is created that stores those 2 most current records that have the same "paymentSystemId".

This is how my look "paymentEvolutionDetail.EvolutionPaymentSystemsByMonth":

var evolutionPaymentSystemsByMonth = new[]
{
    (paymentSystemId: 8, paymentSystem: "PAGO FACIL", transactions: 5, month: 12, year: 2021),
    (paymentSystemId: 4, paymentSystem: "PAGOMISCUENTAS", transactions: 3, month: 12, year: 2021),
    (paymentSystemId: 5, paymentSystem: "RED LINK", transactions: 2, month: 12, year: 2021),
    (paymentSystemId: 4, paymentSystem: "PAGOMISCUENTAS", transactions: 4, month: 1, year: 2022),
    (paymentSystemId: 5, paymentSystem: "RED LINK", transactions: 2, month: 1, year: 2022),
    (paymentSystemId: 8, paymentSystem: "PAGO FACIL", transactions: 3, month: 1, year: 2022),
    (paymentSystemId: 4, paymentSystem: "PAGOMISCUENTAS", transactions: 2, month: 2, year: 2022),
    (paymentSystemId: 5, paymentSystem: "RED LINK", transactions: 1, month: 2, year: 2022),
    (paymentSystemId: 8, paymentSystem: "PAGO FACIL", transactions: 3, month: 2, year: 2022),
};

I need to create a new data where the 2 records are saved with the most current month and year and that have the same paymentSystemId. So, this array would go from length 9 to 6.

I tried to do it with two foreachs but it's so many loops that I got lost. I hope you can help me! From already thank you very much.

This is the example of the array taken from the Google console but when I tried to use the foreachs, I converted the array with ToList()

CodePudding user response:

I need to create a new data where the 2 records are saved with the most current month and year and that have the same paymentSystemId. So, this array would go from length 9 to 6.

With LINQ, it would perhaps look like:

var recent2 = evolutionPaymentSystemsByMonth
  .GroupBy(e => e.paymentSystemId, (k,g) => g.OrderByDescending(e => e.year).ThenByDescending(e => e.month).Take(2))
  .SelectMany(g => g)
  .ToArray();

On the assumption that the data is like ...:


var evolutionPaymentSystemsByMonth = new[]
{
    (paymentSystemId: 8, transactions: 5, month: 12, year: 21, paymentSystem: "PAGO FACIL"),
    (paymentSystemId: 4, transactions: 3, month: 12, year: 21, paymentSystem: "PAGOMISCUENTAS"),
    (paymentSystemId: 5, transactions: 2, month: 12, year: 21, paymentSystem: "RED LINK"),
    (paymentSystemId: 4, transactions: 4, month: 1,  year: 22, paymentSystem: "PAGOMISCUENTAS"),
    (paymentSystemId: 5, transactions: 2, month: 1,  year: 22, paymentSystem: "RED LINK"),
    (paymentSystemId: 8, transactions: 3, month: 1,  year: 22, paymentSystem: "PAGO FACIL"),
    (paymentSystemId: 4, transactions: 2, month: 2,  year: 22, paymentSystem: "PAGOMISCUENTAS"),
    (paymentSystemId: 5, transactions: 1, month: 2,  year: 22, paymentSystem: "RED LINK"),
    (paymentSystemId: 8, transactions: 3, month: 2,  year: 22, paymentSystem: "PAGO FACIL"),
};

Note: it's reorganized to be neater but this has no overall effect on the tuple in this case. Note though that reordering a tuple can affect how it sorts

Footnote: doing that reorg just reminded me something; your LINQ can be made simpler if you reorganize the tuple so what you sort by comes first:

var evolutionPaymentSystemsByMonth = new[]
{
    (year: 21, month: 12, paymentSystemId: 8, transactions: 5, paymentSystem: "PAGO FACIL"),
    (year: 21, month: 12, paymentSystemId: 4, transactions: 3, paymentSystem: "PAGOMISCUENTAS"),
    (year: 21, month: 12, paymentSystemId: 5, transactions: 2, paymentSystem: "RED LINK"),
    (year: 22, month: 1,  paymentSystemId: 4, transactions: 4, paymentSystem: "PAGOMISCUENTAS"),
    (year: 22, month: 1,  paymentSystemId: 5, transactions: 2, paymentSystem: "RED LINK"),
    (year: 22, month: 1,  paymentSystemId: 8, transactions: 3, paymentSystem: "PAGO FACIL"),
    (year: 22, month: 2,  paymentSystemId: 4, transactions: 2, paymentSystem: "PAGOMISCUENTAS"),
    (year: 22, month: 2,  paymentSystemId: 5, transactions: 1, paymentSystem: "RED LINK"),
    (year: 22, month: 2,  paymentSystemId: 8, transactions: 3, paymentSystem: "PAGO FACIL"),
}; 

Now you can just order the tuple by itself descending; because tuples sort in order of their items from left to right, it saves us some to put the year and month first, then we can just OrderByDescending the tuple itself

        var recent2 = evolutionPaymentSystemsByMonth
          .GroupBy(e => e.paymentSystemId, (k, g) => g.OrderByDescending(e=>e).Take(2))
          .SelectMany(g => g)
          .ToArray();

CodePudding user response:

Finally, I managed to do it as follows:

 paymentEvolutionDetail.EvolutionPaymentSystemsByMonth.GroupBy(x => x.PaymentSystemId)
     .SelectMany(x => x.OrderByDescending(g => g.Year)
     .ThenByDescending(g => g.Month)
     .Take(2)).ToList();
  • Related