开发者

Entity Framework

开发者 https://www.devze.com 2023-01-16 12:16 出处:网络
Hi i have Entity Model with lets say 3 entity\'s like so all related of course: 1.-----Costumers-------

Hi i have Entity Model with lets say 3 entity's like so all related of course:

1.-----Costumers-------
  CustomerId
  CustomerName

2.----CustomersOrders-----
  CustomerId
  OrderId
  Total

3.---Orders------
  OrderId
  OrderName

And I want to display orders of some costumer in GridView like so:

----------|----------
 OrderName|Total
----------|-----开发者_StackOverflow社区-----
My order  | 10000

But the problem is entity data source give me order id instead of name how can I use it to get related OrderName for order?

Many thanks....


I assume you have defined the three entities with navigational properties connecting Customers to CustomersOrders and CustomersOrders to Orders. If not use the entity model designer where you can right click your entities and add the relevant associations between the entities. Remember to check that you are generating navigational properties. If the model context is set up like that it is straight forward to query your customer's orders. There are several ways to do that. One of them could be a LINQ expression:

using (var context = new MyEntityContext())
{
    var query = from customerOrder 
                in context.CustomersOrders 
                where customerOrder.CustomerId.Equals(myCustomerId) 
                select customerOrder;

    myGridView.ItemsSource = query.ToList();
}

Here I am assuming you have a customerId in myCustomerId. If you only have a name, not an Id you could do something like:

using (var context = new MyEntityContext())
{
    var query = from customerOrder 
                in context.CustomersOrders.Include("Orders") 
                where customerOrder.Customer.Name.Equals(myCustomerName) 
                select customerOrder;

    myGridView.ItemsSource = query.ToList();
}

In the above I assume that your navigational property from CustomersOrders to Customer is called "Customer". The "Include" makes sure the associated orders get loaded. If not included, the ToList call will assume you don't need the orders and you will not be able to refer to them in your GridView.

In your grid view you could make a binding to the OrderName using Path=Order.OrderName and a binding to Total with Path=Total. Here I am assuming your navigational property from CustomersOrders to Orders is called "Order".

0

精彩评论

暂无评论...
验证码 换一张
取 消