views:

40

answers:

1

I have this challenging task in VB.NET LINQ. I have 2 databases from different servers. I cannot link the databases. The data is retrieved from the databases as :

  • DB1

  • Client_ID Engagement_ID Enabled Description

  • 600 10 True Company1
  • 600 20 False Company2
  • 700 10 True Company3
  • DB2
  • Client_ID Engagement_ID Enabled Description
  • 600 5 True Company1
  • 600 10 False Company2
  • 500 30 True Company3

In the front end I need to add the Engagements of the Clients that are present in DB1 and not present in DB2. I make and API call to add the engagements as I cannot update the database. In this example I need to add the second record of DB1 to DB2 as the client ID 600 exists in DB2 and the engagement ID 20 for Client ID 600 does not exist in DB2. I cannot add the third record of DB1 to DB2 as the client ID 700 does not exists in DB2. I need the LINQ query to return records where DB1.client_id = DB2.Client_ID and DB1.Engagement_ID <> DB2.Engagement_ID. If the LINQ query returns the records in a datatable or some data structure, I can loop through the records and make api calls and pass the client ID, engagement ID , Enabled and description as parameters to update DB2. Please help me with this issue. I tried to do it but LINQ allows equijoins and I cannot get it to work for DB1.client_id = DB2.Client_ID and DB1.Engagement_ID <> DB2.Engagement_ID.

THe T SQL for this task is:

select * from DB1
left join DB2 on DB1.client_ID = DB2.client_ID
and DB1.Engagement_ID = DB2.Engagement_ID
where DB2.CLient_ID is null and DB2.Engagement_ID is null
and DB1.client_id in (select client_id from DB2)

I am trying to convert this T SQL to LINQ

Thanks in advance

+1  A: 

You can add the second join criterion as a Where clause:

Dim q = From e1 In db1
        Join e2 In db2 On e1.Client_ID Equals e2.Client_ID
        Where e1.Engagement_ID <> e2.Engagement_ID
        Select e1, e2
dahlbyk
Thanks for the reply. I tried this before but it does not work.I will post the TSQL for this task:select * from DB1left join DB2 on DB1.client_ID = DB2.client_IDand DB1.Engagement_ID = DB2.Engagement_IDwhere DB2.CLient_ID is null and DB2.Engagement_ID is nulland DB1.client_id in (select client_id from DB2)I am trying to convert this T SQL to LINQ
Nithin Ramachandrappa
@dahlbyk : This gives false results. For example if e1 has keys cid1 and eid1 while e2 has keys cid2 and eid2 this would return a result, when in fact @nithin is looking for the case where cid1 exists in e2 but not the pair cid1 and eid1.
Hogan