1

I would like to get the last 5 values of performance and availability time. To get that I have tried multiple ways, but unfortunately without succes.

I have tried this:

var completeTransactions2 = temp4
                        .Where(x => x.GroupidAvai == x.GroupidPerf && x.KeyTimeAvai == x.KeyTimePerf)
                        .GroupBy(x => x.GroupidAvai)
                        .Select(group => group.OrderByDescending(record => (record.KeyTimeAvai) && (record.KeyTimePerf)).Take(5));

But then I get an error: Operator && cannot be applied to operands of type decimal and decimal.

With that information I have tried something but with result that only one perfTime is being taking the last 5 values and reverse them. Availability is only have one avaitime. This is the query:

var orderedTemp72 = temp4
                        .Where(x => x.GroupidAvai == x.GroupidPerf)
                        .GroupBy(x => new { x.GroupidAvai, x.GroupidPerf })
                        .Select(x => x.OrderByDescending(record => record.KeyTimeAvai)
                        .ThenByDescending(record => record.KeyTimePerf).Take(5).Reverse());

I hope that someone can put me in the right direction for how solving this. If there are question, please don't hesitate to ask.

Thank you for reading.

  • 1
    Try addin parenthesis around the two == expressions. – Lasse V. Karlsen Jul 15 '16 at 08:17
  • 1
    It's not quite clear what are you trying to achieve. If you are filtering the items with `x.GroupidAvai == x.GroupidPerf`, then why you group by `x.GroupidAvai, x.GroupidPerf`. Either filter is incorrect or group by contains redundant fields. Providing sample input data and desired output will help us understanding the goal in order to help you. – Ivan Stoev Jul 15 '16 at 08:43
  • @IvanStoev You are correct. There is some double data, but for now I need that to achieve my end goal here. Sorry for the confusion, but you all helped me with solving this obstacle :) – Fernando S. Kroes Jul 18 '16 at 13:03

4 Answers4

1

I think the issue is your orderbyDescending statement.

Try something like

var completeTransactions2 = temp4
                    .Where(x => x.GroupidAvai == x.GroupidPerf && x.KeyTimeAvai == x.KeyTimePerf)
                    .GroupBy(x => x.GroupidAvai)
                    .Select(group => group.OrderByDescending(record => record.KeyTimeAvai).ThenBy(x => record.KeyTimePerf)).Take(5));
Darren Lamb
  • 1,408
  • 14
  • 18
1

Try using this syntax by Doing OrderByDescending and ThenByDescending

var completeTransactions2 = temp4
                    .Where(x => x.GroupidAvai == x.GroupidPerf && x.KeyTimeAvai == x.KeyTimePerf)
                    .GroupBy(x => x.GroupidAvai)
                    .Select(gro => gro.OrderByDescending(r => r.KeyTimeAvai).ThenByDescending(r => r.KeyTimePerf).Take(5));

OR Doing OrderByDescending and ThenBy

var completeTransactions2 = temp4
                        .Where(x => x.GroupidAvai == x.GroupidPerf && x.KeyTimeAvai == x.KeyTimePerf)
                        .GroupBy(x => x.GroupidAvai)
                        .Select(gro => gro.OrderByDescending(r => r.KeyTimeAvai).ThenBy(r => r.KeyTimePerf).Take(5));

Take a look Here

Community
  • 1
  • 1
Smit Patel
  • 2,508
  • 1
  • 18
  • 41
1

If you want the five greatest of KeyTimeAvai or KeyTimePerf

OrderByDescending(x => x.KeyTimeAvai >= x.KeyTimePerf 
                       ? x.KeyTimeAvai 
                       : x.KeyTimePerf).Take(5);

If you need the five greatest KeyTimeAvai and five greatest KeyTimePerf (so 10 results), I would do something like that.

var groupedTransactions=  temp4
                          .Where(x => x.GroupidAvai == x.GroupidPerf &&
                                      x.KeyTimeAvai == x.KeyTimePerf)
                          .GroupBy(x => x.GroupidAvai);



var greatestByAvai = groupedTransactions
                     .Select(group => group.OrderByDescending(record => record.KeyTimeAvai)
                     .Take(5);
var greatestByPerf = groupedTransactions
                     .Select(group => group.OrderByDescending(record => record.KeyTimePerf)
                     .Take(5);

var allTransactions = greatesByAvai.Concat(greatestByPerf);
Raphaël Althaus
  • 57,325
  • 6
  • 81
  • 109
1

Thank you all for responding/helping me. Really appreciated :) With all your answers I came to the following solution that I can use.

var completeTransactions = temp
                    .Where(x => x.GroupidAvai == x.GroupidPerf && x.KeyTimeAvai == x.KeyTimePerf)
                    .GroupBy(x => x.GroupidAvai)
                        .Select(group => group.OrderByDescending(record => record.KeyTimeAvai).Reverse().Take(5).OrderByDescending(record => record.KeyTimePerf).Reverse().Take(5));