HOW TO UPDATE LINQ DIFFERENT TABLE AFTER JOIN PROCESS

Norman Reedus

Total Post:45

Points:315
Posted by  Norman Reedus
 958  View(s)
Ratings:
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

                };

}

  1. David Miller

    Post:30

    Points:210
    Re: How to update Linq different table after join process

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!