Skip to content

Commit e9e5b2e

Browse files
committed
Fix open issues
- Fixes #43760: Update where clause to filter out some values. Update output comment. - Fixes #43765: Update `let` statement so the query result isn't empty. - Fixes #43766: Update query parameters so the result set isn't empty. - Fixes #43768: Add sample data - Fixes #43790: Audit all samples in this article and ensure all have output. - Fixes #43809: Update samples to include the data sources. While doing this, update the project to .NET 9
1 parent 0e7112c commit e9e5b2e

File tree

6 files changed

+138
-17
lines changed

6 files changed

+138
-17
lines changed

docs/csharp/linq/get-started/query-expression-basics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Query expression basics (LINQ)
33
description: Introduces concepts related to query expressions
4-
ms.date: 03/06/2024
4+
ms.date: 01/16/2025
55
---
66
# Query expression basics
77

docs/csharp/linq/get-started/snippets/SnippetApp/Basics.cs

Lines changed: 93 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
public static class Basics
44
{
55

6-
static readonly int[] scores = [0]; // Max is called on this, so one value is needed
6+
static readonly int[] scores = [
7+
0, 30, 50, 70, 80, 85, 94, 87, 96, 88, 59, 90, 91, 85, 60, 49, 100
8+
];
79

810
// <SourceData>
911
static readonly City[] cities = [
@@ -50,6 +52,10 @@ where score > 80
5052
orderby score descending
5153
select score;
5254
// </basics1>
55+
foreach(var score in highScoresQuery)
56+
{
57+
Console.WriteLine(score);
58+
}
5359
}
5460

5561
public static void Basics2()
@@ -61,6 +67,10 @@ where score > 80
6167
orderby score descending
6268
select $"The score is {score}";
6369
// </basics2>
70+
foreach (var score in highScoresQuery2)
71+
{
72+
Console.WriteLine(score);
73+
}
6474
}
6575

6676
public static void Basics3()
@@ -72,6 +82,7 @@ where score > 80
7282
select score
7383
).Count();
7484
// </basics3>
85+
Console.WriteLine($"highest score: {highScoreCount}");
7586
}
7687

7788
public static void Basics4()
@@ -84,6 +95,7 @@ where score > 80
8495

8596
var scoreCount = highScoresQuery3.Count();
8697
// </basics4>
98+
Console.WriteLine($"highest score: {scoreCount}");
8799
}
88100

89101
public static void Basics5()
@@ -122,7 +134,7 @@ public static void Basics6()
122134
//Query syntax
123135
IEnumerable<City> queryMajorCities =
124136
from city in cities
125-
where city.Population > 100000
137+
where city.Population > 30_000_000
126138
select city;
127139

128140
// Execute the query to produce the results
@@ -132,12 +144,19 @@ where city.Population > 100000
132144
}
133145

134146
// Output:
135-
// City { Population = 120000 }
136-
// City { Population = 112000 }
137-
// City { Population = 150340 }
138-
147+
// City { Name = Tokyo, Population = 37833000 }
148+
// City { Name = Delhi, Population = 30290000 }
149+
139150
// Method-based syntax
140-
IEnumerable<City> queryMajorCities2 = cities.Where(c => c.Population > 100000);
151+
IEnumerable<City> queryMajorCities2 = cities.Where(c => c.Population > 30_000_000);
152+
// Execute the query to produce the results
153+
foreach (City city in queryMajorCities2)
154+
{
155+
Console.WriteLine(city);
156+
}
157+
// Output:
158+
// City { Name = Tokyo, Population = 37833000 }
159+
// City { Name = Delhi, Population = 30290000 }
141160
// </basics6>
142161
}
143162

@@ -158,6 +177,8 @@ from score in scores
158177
// the following returns the same result
159178
highScore = scores.Max();
160179
// </basics7>
180+
Console.WriteLine($"highest score: {highestScore}");
181+
Console.WriteLine($"high Score: {highScore}");
161182
}
162183

163184
public static void Basics7a()
@@ -178,6 +199,14 @@ where city.Population > 10000
178199
select city;
179200
var largeCitiesList2 = largeCitiesQuery.ToList();
180201
// </basics7a>
202+
foreach(var item in largeCitiesList)
203+
{
204+
Console.WriteLine(item.Name + ":" + item.Population);
205+
}
206+
foreach (var item in largeCitiesList2)
207+
{
208+
Console.WriteLine(item.Name + ":" + item.Population);
209+
}
181210
}
182211

183212
public static void Basics8()
@@ -188,16 +217,24 @@ from city in cities
188217
where city.Population > 100000
189218
select city;
190219
// </basics8>
220+
foreach (var city in queryCities)
221+
{
222+
Console.WriteLine(city.Name + ":" + city.Population);
223+
}
191224
}
192225

193226
public static void Basics9()
194227
{
195228
// <basics9>
196229
IEnumerable<Country> countryAreaQuery =
197230
from country in countries
198-
where country.Area > 500000 //sq km
231+
where country.Area > 20 //sq km
199232
select country;
200233
// </basics9>
234+
foreach (var country in countryAreaQuery)
235+
{
236+
Console.WriteLine(country.Name + ":" + country.Area);
237+
}
201238
}
202239

203240
public static void Basics10()
@@ -209,6 +246,10 @@ from city in country.Cities
209246
where city.Population > 10000
210247
select city;
211248
// </basics10>
249+
foreach (var city in cityQuery)
250+
{
251+
Console.WriteLine(city.Name + ":" + city.Population);
252+
}
212253
}
213254

214255
public static void Basics11()
@@ -218,6 +259,14 @@ public static void Basics11()
218259
from country in countries
219260
group country by country.Name[0];
220261
// </basics11>
262+
foreach (var group in queryCountryGroups)
263+
{
264+
Console.WriteLine(group.Key);
265+
foreach (var country in group)
266+
{
267+
Console.WriteLine(country.Name);
268+
}
269+
}
221270
}
222271

223272
public static void Basics12()
@@ -228,6 +277,10 @@ from country in countries
228277
orderby country.Area
229278
select country;
230279
// </basics12>
280+
foreach (var country in sortedQuery)
281+
{
282+
Console.WriteLine(country.Name + ":" + country.Area);
283+
}
231284
}
232285

233286
public static void Basics13()
@@ -241,6 +294,10 @@ from country in countries
241294
Pop = country.Population
242295
};
243296
// </basics13>
297+
foreach (var item in queryNameAndPop)
298+
{
299+
Console.WriteLine(item.Name + ":" + item.Pop);
300+
}
244301
}
245302

246303
public static void Basics14()
@@ -249,7 +306,7 @@ public static void Basics14()
249306
// percentileQuery is an IEnumerable<IGrouping<int, Country>>
250307
var percentileQuery =
251308
from country in countries
252-
let percentile = (int)country.Population / 10_000_000
309+
let percentile = (int)country.Population / 1_000
253310
group country by percentile into countryGroup
254311
where countryGroup.Key >= 20
255312
orderby countryGroup.Key
@@ -272,9 +329,13 @@ public static void Basics15()
272329
// <basics15>
273330
IEnumerable<City> queryCityPop =
274331
from city in cities
275-
where city.Population is < 200000 and > 100000
332+
where city.Population is < 15_000_000 and > 10_000_000
276333
select city;
277334
// </basics15>
335+
foreach (var city in queryCityPop)
336+
{
337+
Console.WriteLine(city.Name + ":" + city.Population);
338+
}
278339
}
279340

280341
public static void Basics16()
@@ -285,12 +346,24 @@ from country in countries
285346
orderby country.Area, country.Population descending
286347
select country;
287348
// </basics16>
349+
foreach (var country in querySortedCountries)
350+
{
351+
Console.WriteLine(country.Name + ":" + country.Area + ":" + country.Population);
352+
}
288353
}
289354

290355
public static void Basics17()
291356
{
292-
string[] categories = [];
293-
Product[] products = [];
357+
string[] categories = ["brass", "winds", "percussion"];
358+
Product[] products = [
359+
new Product("Trumpet", "brass"),
360+
new Product("Trombone", "brass"),
361+
new Product("French Horn", "brass"),
362+
new Product("Clarinet", "winds"),
363+
new Product("Flute", "winds"),
364+
new Product("Cymbal", "percussion"),
365+
new Product("Drum", "percussion")
366+
];
294367

295368
// <basics17>
296369
var categoryQuery =
@@ -302,6 +375,10 @@ join prod in products on cat equals prod.Category
302375
Name = prod.Name
303376
};
304377
// </basics17>
378+
foreach (var item in categoryQuery)
379+
{
380+
Console.WriteLine(item.Category + ":" + item.Name);
381+
}
305382
}
306383

307384
public static void Basics18()
@@ -339,5 +416,9 @@ select student2.ExamScores.Average()
339416
).Max()
340417
};
341418
// </basics19>
419+
foreach (var item in queryGroupMax)
420+
{
421+
Console.WriteLine(item.Level + ":" + item.HighestScore);
422+
}
342423
}
343424
}

docs/csharp/linq/get-started/snippets/SnippetApp/LinqGetStarted.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net9.0</TargetFramework>
66
<RootNamespace>Linq.GetStarted</RootNamespace>
77
<ImplicitUsings>enable</ImplicitUsings>
88
<Nullable>enable</Nullable>

docs/csharp/linq/get-started/snippets/SnippetApp/NullValues.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
11
namespace LinqSamples;
22

3+
// <dataSourceTypes>
34
record Product(string Name, int CategoryID);
45
record Category(string Name, int ID);
6+
// </dataSourceTypes>
7+
58

69
public static class NullValues
710
{
8-
static readonly Category[] categories = [];
9-
static readonly Product[] products = [];
11+
// <source>
12+
static Category?[] categories =
13+
[
14+
new ("brass", 1),
15+
null,
16+
new ("winds", 2),
17+
default,
18+
new ("percussion", 3)
19+
];
20+
21+
static Product?[] products =
22+
[
23+
new Product("Trumpet", 1),
24+
new Product("Trombone", 1),
25+
new Product("French Horn", 1),
26+
null,
27+
new Product("Clarinet", 2),
28+
new Product("Flute", 2),
29+
null,
30+
new Product("Cymbal", 3),
31+
new Product("Drum", 3)
32+
];
33+
// </source>
34+
1035

1136
public static void NullValues1()
1237
{

docs/csharp/linq/get-started/snippets/SnippetApp/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
WriteSeparator(nameof(PartsOfAQuery));
44
PartsOfAQuery();
5+
Console.WriteLine();
56

67
WriteSeparator(nameof(Linq.GetStarted.Basics.Basics1));
78
Linq.GetStarted.Basics.Basics1();
@@ -41,6 +42,7 @@
4142
Linq.GetStarted.Basics.Basics17();
4243
WriteSeparator(nameof(Linq.GetStarted.Basics.Basics18));
4344
Linq.GetStarted.Basics.Basics18();
45+
Console.WriteLine();
4446
WriteSeparator(nameof(Linq.GetStarted.Basics.Basics19));
4547
Linq.GetStarted.Basics.Basics19();
4648

docs/csharp/linq/get-started/write-linq-queries.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Write LINQ queries
33
description: Learn how to write LINQ queries in C#.
4-
ms.date: 04/19/2024
4+
ms.date: 01/16/2025
55
---
66
# Write C# LINQ queries to query data
77

@@ -96,6 +96,13 @@ In some cases, you don't know until run time how many predicates you have to app
9696

9797
:::code language="csharp" source="./snippets/SnippetApp/RuntimeFiltering.cs" id="runtime_filtering_1":::
9898

99+
> [!NOTE]
100+
> This example uses the following data source and data:
101+
102+
:::code language="csharp" source="./snippets/SnippetApp/DataSources.cs" id="basics_datasource":::
103+
104+
:::code language="csharp" source="./snippets/SnippetApp/Basics.cs" id="SourceData":::
105+
99106
You can use control flow statements, such as `if... else` or `switch`, to select among predetermined alternative queries. In the following example, `studentQuery` uses a different `where` clause if the run-time value of `oddYear` is `true` or `false`.
100107

101108
:::code language="csharp" source="./snippets/SnippetApp/RuntimeFiltering.cs" id="runtime_filtering_2":::
@@ -104,6 +111,12 @@ You can use control flow statements, such as `if... else` or `switch`, to select
104111

105112
This example shows how to handle possible null values in source collections. An object collection such as an <xref:System.Collections.Generic.IEnumerable%601> can contain elements whose value is [null](../../language-reference/keywords/null.md). If a source collection is `null` or contains an element whose value is `null`, and your query doesn't handle `null` values, a <xref:System.NullReferenceException> is thrown when you execute the query.
106113

114+
The following example uses these types and static data arrays:
115+
116+
:::code language="csharp" source="./snippets/SnippetApp/NullValues.cs" id="dataSourceTypes":::
117+
118+
:::code language="csharp" source="./snippets/SnippetApp/NullValues.cs" id="source":::
119+
107120
You can code defensively to avoid a null reference exception as shown in the following example:
108121

109122
:::code language="csharp" source="./snippets/SnippetApp/NullValues.cs" id="null_values_1":::

0 commit comments

Comments
 (0)