LINQ - Multiple Datasources


I need this SQL in LINQ.  Note the different databases, db1 & db2.

SELECT * FROM db1.tblA db1tA
INNER JOIN db2.tblA AS db2tA
ON db1tA.ColA = db2tA.ColA

The issues really is the JOIN from DB1 to DB2, LINQ does not like multiple DataContexts.

I am thinking I need to do this in 2 passes

First I will select what I need from DB2.tblA using LINQ and place it somewhere?
And then use what I created above and pass into some more LINQ and join to it.

Would this work?

I should say I am new to C# and this is my first bit of LINQ ever, so very much in at the deep end

Thanks in advance

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi nutnut;

You can send the raw SQL command to the SQL server and have the server execute the command. See the Linq to SQL code below.

// Set up the Linq to SQL DataContext
DataContext1 db = new DataContext1();
// Create a string which has the T-SQL command you wish to execute on the SQL server
string insertStatement = "NSERT INTO db1.tblB SELECT * FROM db1.tblA db1tA INNER JOIN db2.tblA AS db2tAON db1tA.ColA = db2tA.ColA";

// Send the command to the server as a raw SQL command for execution

Open in new window

nutnutAuthor Commented:
Thanks I wish I could but company policy states we have to use LINQ, in case the DB is changed in the future.
Fernando SotoRetiredCommented:
Well then what you stated in your question then should work. Please note that getting the values on the first pass and then passing it in to the SQL server, the inserts will happen one insert per record to be inserted. In other words if there are 100 records to be inserted into the database then there will be 100 commands sent to the server one record per command.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.