Home > DeveloperSection > Forums > How to update Linq different table after join process
Norman Reedus
Norman Reedus

Total Post:45

Points:315
Posted on    January-29-2015 10:15 PM

 ASP.Net C#  LINQ  SQL Server  Join 
Ratings:


 1 Reply(s)
 533  View(s)
Rate this:

I have combined 3 of my tables by using linq join. After that i want to update this table by using data that i get from webform. How can i do this ? My implementation is below

public void updateShoes(Shoe shoe)

{

    var query = from b in db.BrandTbls.AsQueryable()

                join m in db.ShoeModelTbls on b.BrandID equals m.BrandID

                join s in db.ShoeTbls on m.ModelID equals s.ModelID

                where shoe.ShoeID == s.ShoeID

                orderby m.ModelName

                select new

                {

                    s.ShoeID,

                    s.Size,

                    s.PrimaryColor,

                    s.SecondaryColor,

                    s.Quantity,

                    m.ModelName,

                    m.Price,

                    b.BrandName

                };

}



David Miller

Total Post:30

Points:210
Posted on    January-29-2015 10:36 PM

Though your approach is a little bit unclear right now (for e.g. we don't know which entities you are trying to update), however you can modify your code like this,

public void updateShoes(Shoe shoe)

{

    var query = from b in db.BrandTbls.AsQueryable()

            join m in db.ShoeModelTbls on b.BrandID equals m.BrandID

            join s in db.ShoeTbls on m.ModelID equals s.ModelID

            where shoe.ShoeID == s.ShoeID

            orderby m.ModelName

            select new

            {

                Shoe = shoe, Brand = b, Model = m

            };

 

    foreach(var o in query)

    {

        o.Shoe.ColorName = "Black";

        o.Brand.BrandName = "New Branding";

        o.Model.ModelName = "Something else";

    }

 

    db.SaveChanges();

}

Rather picking selected properties from each Entity, you can pick whole entity. Then you can update each entity in a loop as I have doing above.


Don't want to miss updates? Please click the below button!

Follow MindStick