1

I just trying to Linq query update with join condition

 var GsOrderUpdate = (from x in ctxParser.Tbl_Order_Hierarchy
                 join y in ctxParser.tbl_GS_Related_Orders
                 on x.RootOrder equals y.RelatedOrder
                 where y.ParentId == sParentId
                 select new
                 {
                     ID = x.ID,
                     RelatedOrderParentId = y.ID
                 });

foreach (var gs in GsOrderUpdate)
{
    gs.RelatedOrderParentId = gs.ID;
}

ctxParser.SaveChanges();

I am hetting this error:

Property or indexer Anonymous type RelatedOrderParentId cant be assigned to it is read only.

How can resolve this problem.

7
  • 2
    You cannot update anonymous types. You can only update entities. Commented Jan 18, 2016 at 13:49
  • LINQ can be used with many sources of data. Are you using Entity Framework? Commented Jan 18, 2016 at 13:51
  • 1
    Possible duplicate of C# Anonymous types cannot be assigned to -- it is read only Commented Jan 18, 2016 at 13:51
  • what yo wanna do exactly, what is RelatedOrderParentId which you are trying to update?. Commented Jan 18, 2016 at 13:54
  • Anonymous types don't have property set methods. stackoverflow.com/questions/26916695/… Commented Jan 18, 2016 at 13:54

3 Answers 3

1

You can't use anonymous type to update with EntityFramework. If you want to update try so:

var GsOrderUpdate = (from x in ctxParser.Tbl_Order_Hierarchy
             join y in ctxParser.tbl_GS_Related_Orders
             on x.RootOrder equals y.RelatedOrder
             where y.ParentId == sParentId
             select new
             {
                 Parent = x,
                 Child = y
             });

foreach (var gs in GsOrderUpdate)
{
    gs.Child.RelatedOrderParentId = gs.Parent.ID;
}

ctxParser.SaveChanges();
Sign up to request clarification or add additional context in comments.

Comments

0

The entity framework does not track changes in anonymous type objects. It only tracks changes in entities.

You can change your code to do something like this:

var GsOrderUpdate = (from x in ctxParser.Tbl_Order_Hierarchy
                 join y in ctxParser.tbl_GS_Related_Orders
                 on x.RootOrder equals y.RelatedOrder
                 where y.ParentId == sParentId
                 select new
                 {
                     ID = x.ID,
                     Order = y
                 });

foreach (var gs in GsOrderUpdate)
{
    gs.Order.RelatedOrderParentId = gs.ID;
}

ctxParser.SaveChanges();

Now, you include the whole entity that you want to update.

Comments

0

Use Entity class instead of anonymous object:

var GsOrderUpdate = (from x in ctxParser.Tbl_Order_Hierarchy
                 join y in ctxParser.tbl_GS_Related_Orders
                 on x.RootOrder equals y.RelatedOrder
                 where y.ParentId == sParentId
                 select new YourEntityName
                 {
                     ID = x.ID,
                     RelatedOrderParentId = y.ID
                 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.