Search code examples
c#.netentity-framework-6sql-server-2014-express

How can I write this sql query using entity framework and linq


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();
}

Solution

  • 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();