cs11dotnet7/vscode/Chapter11/Ch11Ex02LinqQueries/Program.cs
2022-03-06 10:37:59 +00:00

29 lines
724 B
C#

using Packt.Shared; // Northwind, Customer
using (Northwind db = new())
{
IQueryable<string?> distinctCities =
db.Customers.Select(c => c.City).Distinct();
WriteLine("A list of cities that at least one customer resides in:");
WriteLine($"{string.Join(", ", distinctCities)}");
WriteLine();
Write("Enter the name of a city: ");
string city = ReadLine()!;
IQueryable<Customer> customersInCity = db.Customers.Where(c => c.City == city);
if (customersInCity is null)
{
WriteLine($"No customers found in {city}.");
return;
}
WriteLine($"There are {customersInCity.Count()} customers in {city}:");
foreach (Customer c in customersInCity)
{
WriteLine($" {c.CompanyName}");
}
}