Skip to content

Commit 628cc08

Browse files
committed
Added linq-grouping
1 parent 69aeb68 commit 628cc08

File tree

5 files changed

+228
-90
lines changed

5 files changed

+228
-90
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
*.jar
55
*.war
66
*.ear
7+
.vscode
8+
bin
9+
obj

README.md

Lines changed: 50 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,17 +1206,11 @@ linq28(){
12061206
public void Linq29()
12071207
{
12081208
string[] words = { "cherry", "apple", "blueberry" };
1209-
1210-
var sortedWords =
1211-
from w in words
1212-
orderby w.Length
1213-
select w;
1214-
1209+
1210+
var sortedWords = words.OrderBy(w => w.Length);
1211+
12151212
Console.WriteLine("The sorted list of words (by length):");
1216-
foreach (var w in sortedWords)
1217-
{
1218-
Console.WriteLine(w);
1219-
}
1213+
sortedWords.ForEach(Console.WriteLine);
12201214
}
12211215
```
12221216
```dart
@@ -1242,12 +1236,11 @@ linq29(){
12421236
//c#
12431237
public void Linq30()
12441238
{
1245-
string[] words = { "cherry", "apple", "blueberry" };
1239+
var products = GetProductList();
12461240

1247-
var sortedWords = words.OrderBy(w => w);
1241+
var sortedProducts = products.OrderBy(p => p.ProductName);
12481242

1249-
Console.WriteLine("The sorted list of words:");
1250-
sortedWords.ForEach(Console.WriteLine);
1243+
ObjectDumper.Write(sortedProducts);
12511244
}
12521245
```
12531246
```dart
@@ -1275,9 +1268,9 @@ linq30(){
12751268
public void Linq31()
12761269
{
12771270
string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
1278-
1279-
var sortedWords = words.OrderBy(a => a, new CaseInsensitiveComparer());
1280-
1271+
1272+
var sortedWords = words.OrderBy(a => a, StringComparer.CurrentCultureIgnoreCase);
1273+
12811274
ObjectDumper.Write(sortedWords);
12821275
}
12831276
```
@@ -1305,18 +1298,12 @@ linq31(){
13051298
//c#
13061299
public void Linq32()
13071300
{
1308-
double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };
1309-
1310-
var sortedDoubles =
1311-
from d in doubles
1312-
orderby d descending
1313-
select d;
1314-
1315-
Console.WriteLine("The doubles from highest to lowest:");
1316-
foreach (var d in sortedDoubles)
1317-
{
1318-
Console.WriteLine(d);
1319-
}
1301+
double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };
1302+
1303+
var sortedDoubles = doubles.OrderByDescending(d => d);
1304+
1305+
Console.WriteLine("The doubles from highest to lowest:");
1306+
sortedDoubles.ForEach(Console.WriteLine);
13201307
}
13211308
```
13221309
```dart
@@ -1378,11 +1365,11 @@ linq33(){
13781365
//c#
13791366
public void Linq34()
13801367
{
1381-
string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
1382-
1383-
var sortedWords = words.OrderByDescending(a => a, new CaseInsensitiveComparer());
1384-
1385-
ObjectDumper.Write(sortedWords);
1368+
string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
1369+
1370+
var sortedWords = words.OrderByDescending(a => a, StringComparer.CurrentCultureIgnoreCase);
1371+
1372+
ObjectDumper.Write(sortedWords);
13861373
}
13871374
```
13881375
```dart
@@ -1409,18 +1396,14 @@ linq34(){
14091396
//c#
14101397
public void Linq35()
14111398
{
1412-
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
1413-
1414-
var sortedDigits =
1415-
from d in digits
1416-
orderby d.Length, d
1417-
select d;
1418-
1419-
Console.WriteLine("Sorted digits:");
1420-
foreach (var d in sortedDigits)
1421-
{
1422-
Console.WriteLine(d);
1423-
}
1399+
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
1400+
1401+
var sortedDigits = digits
1402+
.OrderBy(d => d.Length)
1403+
.ThenBy(d => d);
1404+
1405+
Console.WriteLine("Sorted digits:");
1406+
sortedDigits.ForEach(Console.WriteLine);
14241407
}
14251408
```
14261409
```dart
@@ -1455,9 +1438,9 @@ public void Linq36()
14551438
{
14561439
string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
14571440

1458-
var sortedWords =
1459-
words.OrderBy(a => a.Length)
1460-
.ThenBy(a => a, new CaseInsensitiveComparer());
1441+
var sortedWords = words
1442+
.OrderBy(a => a.Length)
1443+
.ThenBy(a => a, StringComparer.CurrentCultureIgnoreCase);
14611444

14621445
ObjectDumper.Write(sortedWords);
14631446
}
@@ -1487,12 +1470,11 @@ linq36(){
14871470
public void Linq37()
14881471
{
14891472
List<Product> products = GetProductList();
1490-
1491-
var sortedProducts =
1492-
from p in products
1493-
orderby p.Category, p.UnitPrice descending
1494-
select p;
1495-
1473+
1474+
var sortedProducts = products
1475+
.OrderBy(p => p.Category)
1476+
.ThenByDescending(p => p.UnitPrice);
1477+
14961478
ObjectDumper.Write(sortedProducts);
14971479
}
14981480
```
@@ -1526,11 +1508,11 @@ public void Linq38()
15261508
{
15271509
string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
15281510

1529-
var sortedWords =
1530-
words.OrderBy(a => a.Length)
1531-
.ThenByDescending(a => a, new CaseInsensitiveComparer());
1532-
1533-
ObjectDumper.Write(sortedWords);
1511+
var sortedWords = words
1512+
.OrderBy(a => a.Length)
1513+
.ThenByDescending(a => a, StringComparer.CurrentCultureIgnoreCase);
1514+
1515+
ObjectDumper.Write(sortedWords);
15341516
}
15351517
```
15361518
```dart
@@ -1558,19 +1540,14 @@ linq38(){
15581540
//c#
15591541
public void Linq39()
15601542
{
1561-
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
1562-
1563-
var reversedIDigits = (
1564-
from d in digits
1565-
where d[1] == 'i'
1566-
select d)
1567-
.Reverse();
1568-
1569-
Console.WriteLine("A backwards list of the digits with a second character of 'i':");
1570-
foreach (var d in reversedIDigits)
1571-
{
1572-
Console.WriteLine(d);
1573-
}
1543+
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
1544+
1545+
var reversedIDigits = digits
1546+
.Where(d => d[1] == 'i')
1547+
.Reverse();
1548+
1549+
Console.WriteLine("A backwards list of the digits with a second character of 'i':");
1550+
reversedIDigits.ForEach(Console.WriteLine);
15741551
}
15751552
```
15761553
```dart
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace linq_grouping
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
Console.WriteLine("Hello World!");
10+
}
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
<RootNamespace>linq_grouping</RootNamespace>
7+
</PropertyGroup>
8+
9+
</Project>

0 commit comments

Comments
 (0)