-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
OrderByDescending-Sample-2.cs
45 lines (40 loc) · 1.62 KB
/
OrderByDescending-Sample-2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Linq;
using System.Collections.Generic;
namespace Ordering
{
public class OrderByDescending2
{
//This sample uses orderby to sort a list of products by name.
//Outputs to the console 77 products:
//ProductId=47, ProductName=Zaanse koeken, Category=Confections, UnitPrice=$9.50
// ...
//ProductID=17 ProductName=Alice Mutton Category=Meat/Poultry UnitPrice=$39.00
public static void QuerySyntaxExample()
{
List<Product> products = Data.Products;
var sortedProducts =
from p in products
orderby p.ProductName descending
select p;
foreach (var p in sortedProducts)
{
Console.WriteLine($"ProductId={p.ProductId}, ProductName={p.ProductName}, Category={p.Category}, UnitPrice={p.UnitPrice:C}");
}
}
//This sample uses orderby to sort a list of words by length.
//Outputs to the console 77 products:
//ProductId=47, ProductName=Zaanse koeken, Category=Confections, UnitPrice=$9.50
// ...
//ProductID=17 ProductName=Alice Mutton Category=Meat/Poultry UnitPrice=$39.00
public static void MethodSyntaxExample()
{
List<Product> products = Data.Products;
var sortedProducts = products.OrderByDescending(p => p.ProductName);
foreach (var p in sortedProducts)
{
Console.WriteLine($"ProductId={p.ProductId}, ProductName={p.ProductName}, Category={p.Category}, UnitPrice={p.UnitPrice:C}");
}
}
}
}