2017-05-15 37 views
0

我知道如何讓客戶和他們的所有訂單:如何獲取Linq to SQL中所有「客戶」對象和僅「打開」的「訂單」?

//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; 

但如何獲取客戶只需與它的命令爲status = "Open"

類:

[Table(Name = "Customers")] 
public class Customer 
{ 
    ...elided... 

    private EntitySet<Order> _orders; 

    [Association(Storage = "_orders", OtherKey = "orderId")] 
    public EntitySet<Order> orders 
    { 
     get 
     { 
      return this._orders; 
     } 

     set 
     { 
      this._orders.Assign(value); 
     } 
    } 
} 

[Table(Name = "Orders")] 
public class Order 
{ 
    ...elided... 
    private string _status; 

    [Column(Storage="_status")] 
    public string _status 
    { 
     get { return this._status; } 
     set { this._status = value; } 
    } 
} 

回答

2

可以直接查詢客戶第一個查詢後,因爲它的一個IQueryable所以

var onlyOpen = customer.where(x=> x.orders._status = "Open"); 
-1

我能夠做到這一點與DataLoadOptions

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 
); 
相關問題