Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Linq query selecting from two tables?

Tags:

c#

sql

linq

wpf

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
like image 469
Hardgraf Avatar asked Mar 19 '23 00:03

Hardgraf


1 Answers

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();
like image 176
DavidG Avatar answered Mar 28 '23 02:03

DavidG