Training
Module
Work with queries in Dynamics 365 Business Central - Training
Learn how to work with the Query object in Dynamics 365 Business Central to join, filter, and aggregate data.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
You can retrieve many objects in one query by using LoadWith.
The following code uses the LoadWith method to retrieve both Customer
and Order
objects.
Northwnd db = new Northwnd(@"northwnd.mdf");
DataLoadOptions ds = new DataLoadOptions();
ds.LoadWith<Customer>(c => c.Orders);
ds.LoadWith<Order>(o => o.OrderDetails);
db.LoadOptions = ds;
var custQuery =
from cust in db.Customers
where cust.City == "London"
select cust;
foreach (Customer custObj in custQuery)
{
Console.WriteLine($"Customer ID: {custObj.CustomerID}");
foreach (Order ord in custObj.Orders)
{
Console.WriteLine($"\tOrder ID: {ord.OrderID}");
foreach (OrderDetail detail in ord.OrderDetails)
{
Console.WriteLine($"\t\tProduct ID: {detail.ProductID}");
}
}
}
Dim db As New Northwnd("c:\northwnd.mdf")
Dim ds As DataLoadOptions = New DataLoadOptions()
ds.LoadWith(Of Customer)(Function(c) c.Orders)
ds.LoadWith(Of Order)(Function(o) o.OrderDetails)
db.LoadOptions = ds
Dim custQuery = From cust In db.Customers() _
Where cust.City = "London" _
Select cust
For Each custObj In custQuery
Console.WriteLine("Customer ID: " & custObj.CustomerID)
For Each ord In custObj.Orders
Console.WriteLine(vbTab & "Order ID: " & ord.OrderID)
For Each detail In ord.OrderDetails
Console.WriteLine(vbTab & vbTab & _
"Product ID: {0}", detail.ProductID)
Next
Next
Next
Training
Module
Work with queries in Dynamics 365 Business Central - Training
Learn how to work with the Query object in Dynamics 365 Business Central to join, filter, and aggregate data.
Documentation
How to: Filter Related Data - ADO.NET
Learn more about: How to: Filter Related Data
How to: Control How Much Related Data Is Retrieved - ADO.NET
Learn more about: How to: Control How Much Related Data Is Retrieved
How to: Filter at the DataContext Level - ADO.NET
Learn more about: How to: Filter at the DataContext Level