-7

can I write this below mentioned query with the same concept in entity framework

update o set o.Name='NewName'
from Organization o
Inner join Guardian g on o.OrgRowId=g.OrgRowId
where g.IsEnabled=1 and g.OrgRowId=1

Please guide!!!

Update

As requested this is what I am able to compose the select query with join, but unable to do the update in the same query

var query = from o in Organizations             
                join g in Guardians on o.OrgRowId equals g.OrgRowId             
                where g.IsEnabled && g.GuardianRowId==1             
                select o;

Update 1 Kilanny, i tried you query and update my one like this, but its not effecting, I am running these queries in Linqpad

using (var context = new DbEntities())
{
    var query = from o in Organizations
                join g in Guardians on o.OrgRowId equals g.OrgRowId
                where g.IsEnabled && g.GuardianRowId == 1
                select o;
    foreach (var item in query)
    {       
        item.Name="New Organization Name";
        context.SaveChanges();
    }   
    query.Dump();
}
Shax
  • 4,207
  • 10
  • 46
  • 62
  • 3
    What have you tried? Did you search how to write `update` and `join` in linq? There are already many questions in this site about it. –  Feb 23 '16 at 11:41
  • Possible duplicate of [C# Joins/Where with Linq and Lambda](http://stackoverflow.com/questions/2767709/c-sharp-joins-where-with-linq-and-lambda) – user1666620 Feb 23 '16 at 11:48
  • @Kilanny, I know how to write join using linq, but confused how to write update query with join in linq and achieve the goal in 1 query, rather writing multiple – Shax Feb 23 '16 at 11:54
  • @Shax You can show your code you have tried so that we can continue it with you –  Feb 23 '16 at 11:56
  • @Kilanny, I have updated my post – Shax Feb 23 '16 at 12:08
  • 1
    @Shax There is probably no way you can achieve it in one step. LINQ is designed to allow querying, not modification. Thus EF allows only query-like, well, 'querying'. Any changes have to be done on the items already retrieved from the database, that are later turned by the database context into a separate CRUD request. If it is a performance-critical task then you may try to use a stored procedure - http://stackoverflow.com/questions/20901419/how-to-call-stored-procedure-in-entity-framework-6-code-first. – Eugene Podskal Feb 23 '16 at 13:28
  • @EugenePodskal, thanks for the guidance, you really explained what I was looking for. – Shax Feb 23 '16 at 14:05

1 Answers1

0
var query = from o in Organizations             
            join g in Guardians on o.OrgRowId equals g.OrgRowId             
            where g.IsEnabled && g.GuardianRowId==1             
            select o;
foreach (var item in query)
   item.Name="NewName";
entities.SaveChanges();