Skip to content

Commit a35966a

Browse files
committed
Added more linq projects
1 parent 67bb052 commit a35966a

File tree

14 files changed

+1013
-352
lines changed

14 files changed

+1013
-352
lines changed

README.md

Lines changed: 283 additions & 352 deletions
Large diffs are not rendered by default.

src/csharp/LinqSamples101.sln

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "linq-conversion", "linq-con
1919
EndProject
2020
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "linq-element", "linq-element\linq-element.csproj", "{91204610-6A4F-4D40-86FC-B413BE2B1E3A}"
2121
EndProject
22+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "linq-generation", "linq-generation\linq-generation.csproj", "{B9A21EFA-98D8-475D-A649-9F15F5C47D8E}"
23+
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "linq-quantifiers", "linq-quantifiers\linq-quantifiers.csproj", "{1FC8ADD4-7394-4A72-8CB5-4AE5C111DAC9}"
25+
EndProject
26+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "linq-aggregate", "linq-aggregate\linq-aggregate.csproj", "{96ED0B27-654A-4D37-992A-183CD2BA15F6}"
27+
EndProject
28+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "linq-miscellaneous", "linq-miscellaneous\linq-miscellaneous.csproj", "{6D7DA36C-69C2-4B12-8BF3-00EC960C0021}"
29+
EndProject
30+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "linq-query", "linq-query\linq-query.csproj", "{B39EA685-84A6-466C-A3DF-B24D93326C44}"
31+
EndProject
32+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "linq-join", "linq-join\linq-join.csproj", "{254997F1-9B09-488B-AC6E-DFFE6791F3E3}"
33+
EndProject
2234
Global
2335
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2436
Debug|Any CPU = Debug|Any CPU
@@ -57,5 +69,29 @@ Global
5769
{91204610-6A4F-4D40-86FC-B413BE2B1E3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
5870
{91204610-6A4F-4D40-86FC-B413BE2B1E3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
5971
{91204610-6A4F-4D40-86FC-B413BE2B1E3A}.Release|Any CPU.Build.0 = Release|Any CPU
72+
{B9A21EFA-98D8-475D-A649-9F15F5C47D8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
73+
{B9A21EFA-98D8-475D-A649-9F15F5C47D8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
74+
{B9A21EFA-98D8-475D-A649-9F15F5C47D8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
75+
{B9A21EFA-98D8-475D-A649-9F15F5C47D8E}.Release|Any CPU.Build.0 = Release|Any CPU
76+
{1FC8ADD4-7394-4A72-8CB5-4AE5C111DAC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
77+
{1FC8ADD4-7394-4A72-8CB5-4AE5C111DAC9}.Debug|Any CPU.Build.0 = Debug|Any CPU
78+
{1FC8ADD4-7394-4A72-8CB5-4AE5C111DAC9}.Release|Any CPU.ActiveCfg = Release|Any CPU
79+
{1FC8ADD4-7394-4A72-8CB5-4AE5C111DAC9}.Release|Any CPU.Build.0 = Release|Any CPU
80+
{96ED0B27-654A-4D37-992A-183CD2BA15F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
81+
{96ED0B27-654A-4D37-992A-183CD2BA15F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
82+
{96ED0B27-654A-4D37-992A-183CD2BA15F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
83+
{96ED0B27-654A-4D37-992A-183CD2BA15F6}.Release|Any CPU.Build.0 = Release|Any CPU
84+
{6D7DA36C-69C2-4B12-8BF3-00EC960C0021}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
85+
{6D7DA36C-69C2-4B12-8BF3-00EC960C0021}.Debug|Any CPU.Build.0 = Debug|Any CPU
86+
{6D7DA36C-69C2-4B12-8BF3-00EC960C0021}.Release|Any CPU.ActiveCfg = Release|Any CPU
87+
{6D7DA36C-69C2-4B12-8BF3-00EC960C0021}.Release|Any CPU.Build.0 = Release|Any CPU
88+
{B39EA685-84A6-466C-A3DF-B24D93326C44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
89+
{B39EA685-84A6-466C-A3DF-B24D93326C44}.Debug|Any CPU.Build.0 = Debug|Any CPU
90+
{B39EA685-84A6-466C-A3DF-B24D93326C44}.Release|Any CPU.ActiveCfg = Release|Any CPU
91+
{B39EA685-84A6-466C-A3DF-B24D93326C44}.Release|Any CPU.Build.0 = Release|Any CPU
92+
{254997F1-9B09-488B-AC6E-DFFE6791F3E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
93+
{254997F1-9B09-488B-AC6E-DFFE6791F3E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
94+
{254997F1-9B09-488B-AC6E-DFFE6791F3E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
95+
{254997F1-9B09-488B-AC6E-DFFE6791F3E3}.Release|Any CPU.Build.0 = Release|Any CPU
6096
EndGlobalSection
6197
EndGlobal

src/csharp/linq-aggregate/Program.cs

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Linq;
4+
using linqshared;
5+
6+
namespace linq_aggregate
7+
{
8+
class Program : ProgramBase
9+
{
10+
static void Main(string[] args)
11+
{
12+
//Linq73();
13+
//Linq74();
14+
//Linq76();
15+
//Linq77();
16+
//Linq78();
17+
//Linq79();
18+
//Linq80();
19+
//Linq81();
20+
//Linq82();
21+
//Linq83();
22+
//Linq84();
23+
//Linq85();
24+
//Linq86();
25+
//Linq87();
26+
//Linq88();
27+
//Linq89();
28+
//Linq90();
29+
//Linq91();
30+
//Linq92();
31+
Linq93();
32+
33+
}
34+
35+
[Category("Aggregate Operators")]
36+
[Description("This sample uses Count to get the number of unique prime factors of 300.")]
37+
static void Linq73()
38+
{
39+
int[] primeFactorsOf300 = { 2, 2, 3, 5, 5 };
40+
41+
var uniqueFactors = primeFactorsOf300.Distinct().Count();
42+
43+
Console.WriteLine($"There are {uniqueFactors} unique prime factors of 300.");
44+
}
45+
46+
[Category("Aggregate Operators")]
47+
[Description("This sample uses Count to get the number of odd ints in the array.")]
48+
static void Linq74()
49+
{
50+
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
51+
52+
int oddNumbers = numbers.Count(n => n % 2 == 1);
53+
54+
Console.WriteLine($"There are {oddNumbers} odd numbers in the list.");
55+
}
56+
57+
[Category("Aggregate Operators")]
58+
[Description("This sample uses Count to return a list of customers and how many orders each has.")]
59+
static void Linq76()
60+
{
61+
var customers = GetCustomerList();
62+
63+
var orderCounts = customers
64+
.Select(cust => new { cust.CustomerID, OrderCount = cust.Orders.Count() });
65+
66+
ObjectDumper.Write(orderCounts);
67+
}
68+
69+
[Category("Aggregate Operators")]
70+
[Description("This sample uses Count to return a list of categories and how many products each has.")]
71+
static void Linq77()
72+
{
73+
var products = GetProductList();
74+
75+
var categoryCounts = products
76+
.GroupBy(prod => prod.Category)
77+
.Select(prodGroup => new { Category = prodGroup.Key, ProductCount = prodGroup.Count() });
78+
79+
ObjectDumper.Write(categoryCounts);
80+
}
81+
82+
[Category("Aggregate Operators")]
83+
[Description("This sample uses Sum to add all the numbers in an array.")]
84+
static void Linq78()
85+
{
86+
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
87+
88+
double numSum = numbers.Sum();
89+
90+
Console.WriteLine($"The sum of the numbers is {numSum}.");
91+
}
92+
93+
[Category("Aggregate Operators")]
94+
[Description("This sample uses Sum to get the total number of characters of all words in the array.")]
95+
static void Linq79()
96+
{
97+
string[] words = { "cherry", "apple", "blueberry" };
98+
99+
double totalChars = words.Sum(w => w.Length);
100+
101+
Console.WriteLine($"There are a total of {totalChars} characters in these words.");
102+
}
103+
104+
[Category("Aggregate Operators")]
105+
[Description("This sample uses Sum to get the total units in stock for each product category.")]
106+
static void Linq80()
107+
{
108+
var products = GetProductList();
109+
110+
var categories = products
111+
.GroupBy(prod => prod.Category)
112+
.Select(prodGroup => new { Category = prodGroup.Key, TotalUnitsInStock = prodGroup.Sum(p => p.UnitsInStock) });
113+
114+
ObjectDumper.Write(categories);
115+
}
116+
117+
[Category("Aggregate Operators")]
118+
[Description("This sample uses Min to get the lowest number in an array.")]
119+
static void Linq81()
120+
{
121+
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
122+
123+
int minNum = numbers.Min();
124+
125+
Console.WriteLine($"The minimum number is {minNum}.");
126+
}
127+
128+
[Category("Aggregate Operators")]
129+
[Description("This sample uses Min to get the length of the shortest word in an array.")]
130+
static void Linq82()
131+
{
132+
string[] words = { "cherry", "apple", "blueberry" };
133+
134+
var shortestWord = words.Min(w => w.Length);
135+
136+
Console.WriteLine($"The shortest word is {shortestWord} characters long.");
137+
}
138+
139+
[Category("Aggregate Operators")]
140+
[Description("This sample uses Min to get the cheapest price among each category's products.")]
141+
static void Linq83()
142+
{
143+
var products = GetProductList();
144+
145+
var categories = products
146+
.GroupBy(prod => prod.Category)
147+
.Select(prodGroup => new { Category = prodGroup.Key, CheapestPrice = prodGroup.Min(p => p.UnitPrice) });
148+
149+
ObjectDumper.Write(categories);
150+
}
151+
152+
[Category("Aggregate Operators")]
153+
[Description("This sample uses Min to get the products with the lowest price in each category.")]
154+
static void Linq84()
155+
{
156+
var products = GetProductList();
157+
158+
var categories = products.GroupBy(prod => prod.Category)
159+
.Select(prodGroup => new {prodGroup, minPrice = prodGroup.Min(p => p.UnitPrice)})
160+
.Select(x => new
161+
{
162+
Category = x.prodGroup.Key,
163+
CheapestProducts = x.prodGroup.Where(p => p.UnitPrice == x.minPrice)
164+
});
165+
166+
ObjectDumper.Write(categories, 1);
167+
}
168+
169+
[Category("Aggregate Operators")]
170+
[Description("This sample uses Max to get the highest number in an array. Note that the method returns a single value.")]
171+
static void Linq85()
172+
{
173+
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
174+
175+
int maxNum = numbers.Max();
176+
177+
Console.WriteLine($"The maximum number is {maxNum}.");
178+
}
179+
180+
[Category("Aggregate Operators")]
181+
[Description("This sample uses Max to get the length of the longest word in an array.")]
182+
static void Linq86()
183+
{
184+
string[] words = { "cherry", "apple", "blueberry" };
185+
186+
int longestLength = words.Max(w => w.Length);
187+
188+
Console.WriteLine($"The longest word is {longestLength} characters long.");
189+
}
190+
191+
[Category("Aggregate Operators")]
192+
[Description("This sample uses Max to get the most expensive price among each category's products.")]
193+
static void Linq87()
194+
{
195+
var products = GetProductList();
196+
197+
var categories = products
198+
.GroupBy(prod => prod.Category)
199+
.Select(prodGroup => new { Category = prodGroup.Key, MostExpensivePrice = prodGroup.Max(p => p.UnitPrice) });
200+
201+
ObjectDumper.Write(categories);
202+
}
203+
204+
[Category("Aggregate Operators")]
205+
[Description("This sample uses Max to get the products with the most expensive price in each category.")]
206+
static void Linq88()
207+
{
208+
var products = GetProductList();
209+
210+
var categories = products.GroupBy(prod => prod.Category)
211+
.Select(prodGroup => new {prodGroup, maxPrice = prodGroup.Max(p => p.UnitPrice)})
212+
.Select(x => new
213+
{
214+
Category = x.prodGroup.Key,
215+
MostExpensiveProducts = x.prodGroup.Where(p => p.UnitPrice == x.maxPrice)
216+
});
217+
218+
ObjectDumper.Write(categories, 1);
219+
}
220+
221+
[Category("Aggregate Operators")]
222+
[Description("This sample uses Average to get the average of all numbers in an array.")]
223+
static void Linq89()
224+
{
225+
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
226+
227+
var averageNum = numbers.Average();
228+
229+
Console.WriteLine($"The average number is {averageNum}.");
230+
}
231+
232+
233+
[Category("Aggregate Operators")]
234+
[Description("This sample uses Average to get the average length of the words in the array.")]
235+
static void Linq90()
236+
{
237+
string[] words = { "cherry", "apple", "blueberry" };
238+
239+
double averageLength = words.Average(w => w.Length);
240+
241+
Console.WriteLine($"The average word length is {averageLength} characters.");
242+
}
243+
244+
[Category("Aggregate Operators")]
245+
[Description("This sample uses Average to get the average price of each category's products.")]
246+
static void Linq91()
247+
{
248+
var products = GetProductList();
249+
250+
var categories = products
251+
.GroupBy(prod => prod.Category)
252+
.Select(prodGroup => new { Category = prodGroup.Key, AveragePrice = prodGroup.Average(p => p.UnitPrice) });
253+
254+
ObjectDumper.Write(categories);
255+
}
256+
257+
[Category("Aggregate Operators")]
258+
[Description("This sample uses Aggregate to create a running product on the array that calculates the total product of all elements.")]
259+
static void Linq92()
260+
{
261+
double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };
262+
263+
var product = doubles.Aggregate((runningProduct, nextFactor) => runningProduct * nextFactor);
264+
265+
Console.WriteLine($"Total product of all numbers: {product}");
266+
}
267+
268+
[Category("Aggregate Operators")]
269+
[Description(@"This sample uses Aggregate to create a running account balance that
270+
subtracts each withdrawal from the initial balance of 100, as long as
271+
the balance never drops below 0.")]
272+
static void Linq93()
273+
{
274+
double startBalance = 100.0;
275+
276+
int[] attemptedWithdrawals = { 20, 10, 40, 50, 10, 70, 30 };
277+
278+
var endBalance = attemptedWithdrawals
279+
.Aggregate(startBalance,
280+
(balance, nextWithdrawal) =>
281+
((nextWithdrawal <= balance) ? (balance - nextWithdrawal) : balance));
282+
283+
Console.WriteLine($"Ending balance: {endBalance}");
284+
}
285+
286+
}
287+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
<RootNamespace>linq_aggregate</RootNamespace>
7+
</PropertyGroup>
8+
9+
<Import Project="..\linq-shared\linq-shared.projitems" Label="Shared" Condition="Exists('..\linq-shared\linq-shared.projitems')" />
10+
</Project>

src/csharp/linq-generation/Program.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Linq;
4+
using linqshared;
5+
6+
namespace linq_generation
7+
{
8+
class Program : ProgramBase
9+
{
10+
static void Main(string[] args)
11+
{
12+
//Linq65();
13+
Linq66();
14+
}
15+
16+
[Category("Generation Operators")]
17+
[Description(@"This sample uses Range to generate a sequence of numbers from 100 to 149
18+
that is used to find which numbers in that range are odd and even.")]
19+
static void Linq65()
20+
{
21+
var numbers = Enumerable.Range(100, 50)
22+
.Select(n => new { Number = n, OddEven = n % 2 == 1 ? "odd" : "even" });
23+
24+
numbers.ForEach((n) => Console.WriteLine("The number {0} is {1}.", n.Number, n.OddEven));
25+
}
26+
27+
[Category("Generation Operators")]
28+
[Description("This sample uses Repeat to generate a sequence that contains the number 7 ten times.")]
29+
static void Linq66()
30+
{
31+
var numbers = Enumerable.Repeat(7, 10);
32+
33+
numbers.ForEach(Console.WriteLine);
34+
}
35+
36+
}
37+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
<RootNamespace>linq_generation</RootNamespace>
7+
</PropertyGroup>
8+
9+
<Import Project="..\linq-shared\linq-shared.projitems" Label="Shared" Condition="Exists('..\linq-shared\linq-shared.projitems')" />
10+
</Project>

0 commit comments

Comments
 (0)