Hardgraf
Hardgraf

Reputation: 2626

Update Linq query selecting from two tables?

I am trying to translate a sql query to Linq to be called from a command in my C# WPF application. I've tried to use Linqer but the query won't translate. I'm new to Linq and have been reading around. Do you use a Linq 'JOIN' to solve this?

update P 
set P.versionid=a.versionid
from tbPublicationArticles P, tbarticles a
where P.articleid=a.articlesid

Upvotes: 3

Views: 4559

Answers (1)

DavidG
DavidG

Reputation: 118987

First get your data, joining the 2 tables together:

var results = from p in db.tbPublicationArticles
              join a in db.tbarticles on p.articleid = a.articlesid
              select new { p, a };

Now you loop through the results and make the changes you require:

foreach(var item in results)
{
    item.p.versionid = item.a.versionid;
}

And don't forget to save your changes:

db.SaveChanges();

Upvotes: 6

Related Questions