C# 如何取回所有“客户”对象以及在LINQtoSQL中仅“打开”的“订单”?
我知道如何获得客户及其所有订单:C# 如何取回所有“客户”对象以及在LINQtoSQL中仅“打开”的“订单”?,c#,sql-server,linq,C#,Sql Server,Linq,我知道如何获得客户及其所有订单: //Get data context for the SQL Server connection DataContext connection = new DataContext(getConnection()); //Customer table to query against Table<Customer> customers = connection.GetTable<Customer>(); //This query giv
//Get data context for the SQL Server connection
DataContext connection = new DataContext(getConnection());
//Customer table to query against
Table<Customer> customers = connection.GetTable<Customer>();
//This query gives all customers and ALL their orders
IQueryable<Customer> query =
from customer in customer
where customer.id == custId select customer;
您可以在第一次查询后直接查询客户,因为它是一个IQueryable so
var onlyOpen = customer.where(x=> x.orders._status = "Open");
我可以使用DataLoadOptions完成此操作:
var onlyOpen = customer.where(x=> x.orders._status = "Open");
public virtual void addAssociationFilter<T>(Expression<Func<T, object>> filter, DataContext dataContext)
{
//We'll use this to restrict our main object's entity list property
DataLoadOptions restrictList = new DataLoadOptions();
//We only want ops that are current or finished
restrictList.AssociateWith<T>(filter);
//Set on our context
dataContext.LoadOptions = restrictList;
}
//Call like:
addAssociationFilterJCustomer>(
cust => cust.orders.Where(o => o.status == "Open"),
dataContext
);