1 Replies - 597 Views - Last Post: 24 April 2014 - 01:34 AM Rate Topic: -----

#1 c#think#:)  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 114
  • Joined: 17-March 10

LinqToSql DataLoadOptions LoadWith question...

Posted 09 April 2014 - 08:59 AM

Hi there gurus!

I have a question that should be an easy one, but can't figure it out myself.
Here goes:
Imagine I have a table 'Client' in which there are two foreign keys 'PrimaryManagerID' and 'SecondaryManagerID'. Both of them referring to table 'Manager' field ManagerID. So, each client must have two managers (one is primary and the other one is secondary)

Now, I want to display in DataGrid some client with his managers. So, on loading data from Database I use:
(code below may have some mistakes, since I write it on the run, just for you to get the picture)
using(DataContext dc = new DataContext(con))
{
DataLoadOptions dlo = new DataLoadOptions();

dlo.LoadWith<Client>(d => w.Manager);
dc.LoadOptions = dlo;
}



Now, in XAML DataGrid Column binding(window datacontext is assigned):
Binding="{Binding Path=Manager.Name}"



The question is: Which Manager's name will output? Primary or secondary?
My lack of knowledge limits me here.

In regular SQL I would solve it like this
LEFT OUTER JOIN MANAGER AS PrimaryMgr ON PrimaryMgr.ManagerID = Client.PrimaryManagerID
LEFT OUTER JOIN MANAGER AS SecondaryMgr ON SecondaryMgr.ManagerID = Client.SecondaryManagerID



But how to use alias in linq?

Any help is appreciated!

This post has been edited by c#think#:): 09 April 2014 - 09:01 AM


Is This A Good Question/Topic? 0
  • +

Replies To: LinqToSql DataLoadOptions LoadWith question...

#2 c#think#:)  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 114
  • Joined: 17-March 10

Re: LinqToSql DataLoadOptions LoadWith question...

Posted 24 April 2014 - 01:34 AM

Hi everybody!

Wow, not a single reply for a such long period... Oh well, maybe it is me, who asked the question in incorrect way or something...

Anyway, I managed to solve it and as I assumed it was very easy. I'll post it here for those, who has the same issue.
In DBML file (a place where you have all your data tables) I created 2 associations(relations) between tables Client and Manager on previously mentioned fields. All I had to do is to name these associations differently. In my case these were PrimaryMNG and SecondaryMNG. Then when pulling data from DB:
using(DataContext dc = new DataContext(con))  
{  
    DataLoadOptions dlo = new DataLoadOptions();      

    dlo.LoadWith<Client>(c => c.PrimaryMNG);  
    dlo.LoadWith<Client>(c => c.SecondaryMNG);  

    dc.LoadOptions = dlo;
} 


Then in XAML for Client's Primary or Secondary Manager:
<DataGridTextColumn Header="Primary manager" Binding="{Binding Path=PrimaryMNG.Name}" />
<DataGridTextColumn Header="Secondary manager" Binding="{Binding Path=SecondaryMNG.Name}" />



That's it.

Happy Coding everybody
C#Think#:)

This post has been edited by c#think#:): 24 April 2014 - 01:44 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1