Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Cool: Using FK relations (Associations) instead of Joins...
Message
From
25/06/2008 12:58:20
 
 
To
All
General information
Forum:
ASP.NET
Category:
LINQ
Title:
Cool: Using FK relations (Associations) instead of Joins...
Miscellaneous
Thread ID:
01326680
Message ID:
01326680
Views:
51
I watched a Linq video by Scott GU and learned this cool thing about using the Associations that Linq-to-Sql automatically creates in your data model if the proper PK and Forign Key relations exits in you SQL server tables. (You can add them manually in the dmbl designer if they are not in the SQL Server already.)

Anway, this allows you to *directly* refer to the related table, *without* defining the corresponding Joins in your Linq code:

Here's what I mean:

Most commonly demoed way, using Joins in the Linq call:
            var subq = (from p in db.job_info
                        join c in db.customers on p.cust_num equals c.custno
                        where p.status == 'A'
                        orderby c.company
                        select c.custno).Distinct();
Easier way using Associations in the dbml:
            var subq = (from p in db.job_info
                        where p.status == 'A'
                        orderby p.customer_source.company
                        select p.customer_source.custno).Distinct();
See the jump?... p.customer_source.company in Orderby and p.customer_source.custno in Select ?

I think is only works from the child to the parent. You cannot go frmo the parent to the child, I think.


This would sure keep you from defining the Join dozens of times in an app if you will invest the time to put the Associations in the dbml.
Next
Reply
Map
View

Click here to load this message in the networking platform