views:

81

answers:

3

I create a EDM using my database and saw that while it was able to make relations like

Customers.First().Orders

there is nothing like

Customer.First().Orders.FindOrderByOrderID()

Customer.First().Orders.FindOrderByOrderName()

etc.

Maybe I was expecting it to be like that and that's how it doesnt work and I will probably just have to LINQ to entities and create mangers that handle business logic in them.

or does LINQ to SQL do this?

+2  A: 

You can use LINQ with the Orders property, like this:

var order = Customer.First().Orders.FirstOrDefault(o => o.OrderId == someId);

This will return null if there was no matching Order.

SLaks
A: 

With LINQ to Entities you can do this. Such as the example SLaks posted above.

If you're feeling adventurous, it is possible to write a function to do this. Since Entity Framework knows which column is the primary key it is possible to write a function like GetEntityByPrimaryKey.

CodeProject has an article regarding the repository pattern and if you search for "SelectByKey" you'll see their implementation of that. I use something similar and find it very helpful.

itchi
A: 

The idea is that you can use LINQ to write more flexible queries. Note, however, that you could probably so something like FindOrderByOrderName in .NET 4.0 by creating your own dynamic type (not trivial), that responded to calls following this pattern by building a LINQ query automatically. You'd lose intellisense, of course, and I doubt it would be worth it, but it could be done.

Marc Gravell
If I had to do this, I'd probably use a custom T4 template to do this, instead. It's easier, and you get IntelliSense. OTOH, I'd probably not find it worth the effort, since I'd rather just see the LINQ.
Craig Stuntz