Use LINQ to Select Data Within
Collections
Paul D. Sheriff
Business / IT Consultant
psheriff@pdsa.com www.pdsa.com
Module Goals
An overview of the console application
Selecting all data
Selecting specific columns
Building an anonymous class
The Demo Classes
Sample Console Application
Console app runs
each sample
Using a Console
Application
Classes to help us
query
Using .NET 6
Product and Sale
Entity Classes.
Classes to Build
Collections
Product Entity Class
public partial class Product { Represents a "Product"
Each property would have a { get; set; }
public int ProductID
Eliminated here for brevity
public string Name
public string Color
public decimal StandardCost
public decimal ListPrice
public string Size
}
Product Repository Class
public partial class ProductRepository { Class to retrieve collection of product data
public static List<Product> GetAll() { Method to retrieve all products from a data source
return new List<Product> {
new Product { Using hard-coded values for this course
ProductID = 680,
Name = "HL Road Frame",
Color = "Black",
StandardCost = 1059.31M,
ListPrice = 1431.50M,
Size = "58",
},
...
View Model Base Class
public class ViewModelBase { Base class used by SamplesViewModel class
public void GetProducts() { Method to retrieve set of Product objects
return ProductRepository.GetAll();
public void GetSales() { Method to retrieve set of SalesOrder objects
return SalesOrderRepository.GetAll();
}
Overloaded methods to display Products, SalesOrder
public Display(*) { objects, as well as many different lists
}
SamplesViewModel Class
public class SamplesViewModel : View model class used to teach LINQ samples
ViewModelBase {
public void GetAllQuery() { One method to illustrate LINQ query syntax
public void GetAllMethod() { One method to illustrate LINQ method syntax
}
SamplesViewModel Method
public List<Product> GetAllQuery() Method returns a set of data
List<Product> products = Build a collection to query
GetProducts();
Create variable to hold results
List<Product> list;
// Write Query Syntax Here
Write a query using LINQ methods
list = (from prod in products
select prod).ToList();
return list; Return the results
}
Program.cs
using LINQSamples; Bring in LINQSamples namespace
// Create instance of view model Create instance of sample view model class
SamplesViewModel vm = new();
// Call Sample Method
Call the sample method you just wrote
var result = vm.GetAllQuery();
// Display Results
vm.Display(result); Display results in console
Selecting
Demo
Select all items using LINQ
Sample Methods
public List<Product> GetAllQuery() {
There are many methods in the SamplesViewModel
List<Product> products = class
GetProducts();
List<Product> list = new(); I had to set the 'list' variable to new() so all methods
could compile
... REST OF CODE HERE Once you set 'list' to the results of the query, you can
eliminate the '= new()'
}
public List<Product> GetAllMethod() {
List<Product> products =
GetProducts();
List<Product> list = new();
... REST OF CODE HERE
}
Sample Methods
public List<Product> GetAllQuery() { In a real-world scenario, you can eliminate the 'list'
variable
List<Product> products =
GetProducts();
return (from prod in products) Simply return the results of the query
.ToList() While training, I will be showing you the results of the
'list' variable in the debugger, so it makes it easier to add
the variable in this course
}
Why Show Query and Method Syntax?
You may run across
Some queries must samples or articles So, you should
be done with the where the author know both
method syntax used one or the methods
other
Demo
Get a single column
Demo
Get specific columns
Demo
Build an anonymous class
Module
Query syntax is readable, if a little verbose
Summary
Method syntax is very compact
Can select single properties
Can select multiple properties
Project new columns using anonymous
classes
Up Next:
Use LINQ to Order Data