Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
It could be a simple question but with my code here i can't use orderby desc and that's my problem

i will be glad for an explanation which is always helping me

look at what I have tried :

What I have tried:

public static void GetDruckerNameAndAnzahl (List<Queue> input)
       {
           var model =
           from print in input
           where print.LastStatus == "Printed"
           group print by print.PrinterName into PrintGroupPrinted
           select new
           {
               PrinterName = PrintGroupPrinted.Key,
               TotalCount = PrintGroupPrinted.Count(),

           };

           foreach (var item in model)
           {
               Console.WriteLine($"{item.PrinterName}:{item.TotalCount}");
           }


       }
Posted
Updated 6-Sep-19 0:35am
Comments
JayantaChatterjee 6-Sep-19 6:29am    
On which property do you want to Order by desc in your Object list?

Check this (lambda version):
C#
public static void GetDruckerNameAndAnzahl (List<Queue> input)
       {
           var model = input
           .Where(print=> print.LastStatus == "Printed")
           .GroupBy(print=> print.PrinterName)
           .OrderByDescending(grp=> grp.Count())
           .Select(grp=> new
           {
               PrinterName = grp.Key,
               TotalCount = grp.Count(),

           })
           .ToList();

           foreach (var item in model)
           {
               Console.WriteLine($"{item.PrinterName}:{item.TotalCount}");
           }


       }


For further details, please see: Enumerable.OrderByDescending Method (System.Linq) | Microsoft Docs[^]

Non-lambda version:
C#
var model =
from print in input
where print.LastStatus == "Printed"
group print by print.PrinterName into PrintGroupPrinted
orderby PrintGroupPrinted.Count() descending
select new {...}
 
Share this answer
 
v2
Comments
[no name] 6-Sep-19 6:54am    
yes its working . i thank you
Maciej Los 6-Sep-19 7:25am    
You're very welcome.
In LINQ:-
C#
public static void GetDruckerNameAndAnzahl (List<Queue> input)
       {
           var model =
           from print in input
           where print.LastStatus == "Printed"
           group print by print.PrinterName into PrintGroupPrinted
orderby PrintGroupPrinted.Count() descending
           select new
           {
               PrinterName = PrintGroupPrinted.Key,
               TotalCount = PrintGroupPrinted.Count(),

           };

           foreach (var item in model)
           {
               Console.WriteLine($"{item.PrinterName}:{item.TotalCount}");
           }


       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900