Click here to Skip to main content
15,888,031 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a database with several tables linked using associations. Is it possible to query the database to extract records from tables two tiers down using the association? See code below?

I am trying to get values from all three tables i.e from table BOOK extract P_C and STRIKE, from table OPTIONS extract OPT_MULTIPLIER and OPT_EXPIRY, and finally from table SKEW extract SKEW_A.

I can get records one tier down, but I don't know how to get records from a table two tiers down. Here "http://imgur.com/D7kh1oE" is a link to the mapping image.

What I have tried:

C#
using (lnkTRADERDataContext DATActx = new lnkTRADERDataContext())
{

    var qryBOOK = from B in DATActx.BOOKs where B.DEAL == 1
                  select new clsTRADER()
                  {
                      P_C = B.P_C,       //main table
                      STRIKE = B.STRIKE, //main table
                      OPT_EXP = B.OPTIONs.Single(X => X.OPT_EXPIRY),        //one tier down
                      OPT_MULT = B.OPTIONs.Single(X => X.OPT_MULTIPLIER),   //one tier down
                      SKEW_A = B.OPTIONs.SKEWs.Single(X => X.SKEW_A)        // two tiers down
               };
}
Posted
Updated 23-Mar-16 2:33am
v8
Comments
Trader999 23-Mar-16 7:02am    
CHill60 - thanks for the edit. How do you put the text in code format?
Garth J Lancaster 23-Mar-16 7:18am    
he hasnt posted as such - so I doubt he'll see your question ... when I do the same edit on people's questions, I usually select the code in the editor, click the 'code' link at the top of the edit pane, then select the language, then 'ok' (you can us the pre tags directly once you know what they are)
Trader999 23-Mar-16 7:29am    
thanks Garth, I'll format it myself next time
Sascha Lefèvre 23-Mar-16 7:43am    
It's certainly possible. But in order to show you it's essential to know a) the model and b) your predicates for what to select (best explain them in normal language).
Sascha Lefèvre 24-Mar-16 9:11am    
Let's continue our discussion up here.

>> For example BOOK is linked to OPTIONS via the CONTRACT field. If one of the records in BOOK has a value of ABC123 for CONTRACT, and only one record in OPTIONS has a value of ABC123 for CONTRACT then a query should produce a record with all BOOK fields and all OPTIONS fields where BOOK.CONTRACT==OPTIONS.CONTRACT

I don't think you actually need this but, just for illustration, that would be:

from B in DATActx.BOOKs
join O in DATActx.OPTIONs on B.CONTRACT equals O.CONTRACT
select new { B, O };

Or the select clause like this instead:
select new {
    Contract = B.CONTRACT,
    BookID = B.ID,
    BookDeal = B.DEAL,
    ...
    OptionID = O.ID,
    OptionExchange = O.OPT_EXCHANGE,
    ...
};

>> I should make OPTIONS.CONTRACT the primary key (and its linked to BOOK.CONTRACT in a one to many)

So one OPTION can have multiple BOOKs? In that case I have to apologize, I could have seen this earlier from your diagram but I got mislead by the name of the property "OPTIONs" of BOOK: If it's a single reference (foreign key) to one OPTION (or null) then you shouldn't use plural there (and the same goes for OPTION.SKEWs, name it SKEW instead.)

>> [..] but BOOK has nulls and this raised an error

Is there a table in which CONTRACT is unique and non-nullable? Your diagram looks like it might be the table SKEW. Then you could make CONTRACT the primary key there. And then I would remove CONTRACT from BOOK because it can be retrieved from OPTION, if there is one, and if there is no OPTION for a book it probably has no CONTRACT anyway, right? That would be database normalization (don't duplicate information), which sometimes is beneficial to ignore but you should have a good reason for it.

Does the following query work? If yes, I would assume it's the solution to your question:

var qryBOOK =
from B in DATActx.BOOKs where B.DEAL == 1
select new clsTRADER()
{
    P_C = B.P_C,
    STRIKE = B.STRIKE,
    OPT_EXP = B.OPTIONs.OPT_EXPIRY,
    OPT_MULT = B.OPTIONs.OPT_MULTIPLIER,
    SKEW_A = B.OPTIONs.SKEWs.SKEW_A
};

1 solution

In a way, its no different from SQL - you use a join clause - see this join clause (C# Reference)[^]
 
Share this answer
 
Comments
Trader999 23-Mar-16 7:01am    
thanks Garth, I was hoping that with the mapping (associations) I didn't need to use joins. For example see question on Stackoverflow. http://stackoverflow.com/questions/6749740/how-to-access-associations-in-a-linq-query
Garth J Lancaster 23-Mar-16 7:04am    
well ... my thoughts are, if you explicity specify the joins, it makes your code readable...... unless you include 'what joins to what' in your code comments, how does some other poor schmo reading your code figure out the relationships ?
Garth J Lancaster 23-Mar-16 7:06am    
reply # 2 - last thought - make it work, then make it pretty :-)
Trader999 23-Mar-16 7:09am    
I agree with make it work and then make it pretty. However the linq visual mapping is a really easy way to see the relationships and easier to change than hard code...so I was hoping for a way to use associations instead of joins but can't get the syntax right.
Garth J Lancaster 23-Mar-16 7:20am    
sorry, I guess I'd have to 'see it' pictorially - I dont get the B.OPTIONS.SKEWs

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900