none
How to select for update with joint RRS feed

  • Question

  • I am new for LINQ to SQL.  I am trying to do a select for update using two tables. The two tables have a FK relationship on an ID column from table1. So what I am trying to do here is that when a specific value changed on TABLE1 via a FORM  (Example: "ORANGE" for name column on table1) not to update table2 corresponding ID value. Currently it updates corresponding values in table2 due to the FK relationship. So my question is on how not to enforce the FK relationship when changing to a specific value if this makes sense.

     Here is what I am working on. I am getting property or indexer anonymous type can't be assigned, read only error under loc.Name section. Can someone please provide some guidance or direction on how to accomplish this task?

     var Table1_Table2= (from g in db.Table1

                      from p in db.Table2

                      where g.Id == p.Id

                      && g.Name != "ORANGE"

                      select new {g.Name, g.Id,p.Id}

    foreach (var loc in Table1_Table2 )

    {

                    loc.Name = Request["Location" + loc.Id];

                    loc.Group = Request[("LocationGroup" + loc.Id)].ToString();

                }

                db.SubmitChanges();

     var Table1_Table2= (from g in db.Table1 
                      from p in db.Table2
                      where g.Id == p.Id
                      && g.Name != "ORANGE"
                      select new {g.Name, g.Id,p.Id}
    foreach (var loc in Table1_Table2 )
    {
                    loc.Name = Request["Location" + loc.Id];
                    loc.Group = Request[("LocationGroup" + loc.Id)].ToString();
                }
                db.SubmitChanges();
             
            }

    Saturday, November 30, 2013 9:36 PM

Answers

  • Sorry that I may not make it clearly. The purpose I create the OrderDTO is just to read data rather than modify data.

     >>select new OrderDTO() { GId = g.Id, Name = g.Name, PId = p.Id }).ToList();

    This will be a View, however we can not update a view in database. To modify the data, we have to use the entity classes created by entity framework provider.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, December 5, 2013 7:31 AM
    Moderator

All replies

  • I am new for LINQ to SQL.  I am trying to do a select for update using two tables. The two tables have a FK relationship on an ID column from table1. So what I am trying to do here is that when a specific value changed on specific column on TABLE1 via a FORM  (Example: if "ORANGE" on name column changed on table1) not to update table2 corresponding ID value. Currently it updates corresponding values on table2 due to the FK relationship. So my question is on how not to enforce the FK relationship when changing to a specific value on a column, Does this makes sense?

    Here is what I have. I am getting property or indexer anonymous, type can't be assigned, read only error under loc.Name section. Can someone please provide some guidance or direction on how to accomplish this task?

     var Table1_Table2= (from g in db.Table1

                      from p in db.Table2

                      where g.Id == p.Id

                      && g.Name != "ORANGE"

                      select new {g.Name, g.Id,p.Id}

    foreach (var loc in Table1_Table2 )

    {

                    loc.Name = Request["Location" + loc.Id];

                    loc.Group = Request[("LocationGroup" + loc.Id)].ToString();

                }

                db.SubmitChanges();


    • Edited by Kidus05 Sunday, December 1, 2013 12:24 AM
    • Merged by Fred BaoModerator Monday, December 2, 2013 6:39 AM They are the same.
    Saturday, November 30, 2013 9:40 PM
  • Hello,

    >> I am getting property or indexer anonymous type can't be assigned, read only error under loc.Name section.

    It will be. A mutable anonymous type is that it would be dangerous to use one as a hash key. Anonymous type is designed with the assumptions that (1) we are going to use them as the keys in equijoins in LINQ query comprehensions, and (2) in LINQ-to-Objects and other implementations, joins will be implemented using hash tables.

    >> Can someone please provide some guidance or direction on how to accomplish this task?

    For this, we have to make a nominal type like below:

      var result = (from order in db.Orders
    
                                  from od in db.OrderDetails
    
                                  where order.OrderID == od.OrderID
    
                                  && order.OrderID == 1
    
                                  select new OrderDTO() { OrderID = order.OrderID, OrderCode = order.OrderCode, OrderName = order.OrderName, }).ToList();
    

    Then the properties can be changed:

        foreach (var r in result)
    
                    {
    
                        r.OrderCode = 2;
    
                    }
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 2, 2013 6:59 AM
    Moderator
  • thanks for the response.

    ok, i have been trying to get this to work using the above example without any luck. I careated a method for OrderDTO()

    public class OrderDTO        {
                public string Name { get; set; }
                public int? GId { get; set; }
                public int? PId { get; set; }
            }
    select new OrderDTO() { GId = g.Id, Name = g.Name, PId = p.Id }).ToList();
     foreach (var id in results)
                {
                    
                    id.PId = null;
                }
    Then I do  db.SubmitChanges(), bur forsome reason it is not saving the change. could this be an issue with my class setup above?

    Wednesday, December 4, 2013 6:41 PM
  • Sorry that I may not make it clearly. The purpose I create the OrderDTO is just to read data rather than modify data.

     >>select new OrderDTO() { GId = g.Id, Name = g.Name, PId = p.Id }).ToList();

    This will be a View, however we can not update a view in database. To modify the data, we have to use the entity classes created by entity framework provider.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, December 5, 2013 7:31 AM
    Moderator