LINQ to SQL:DataContext.SubmitChanges not updating immediately

Posted by aximili on Stack Overflow See other posts from Stack Overflow or by aximili
Published on 2011-01-14T03:44:44Z Indexed on 2011/01/14 3:53 UTC
Read the original article Hit count: 303

Filed under:
|
|
|

I have a funny problem.

Doing DataContext.SubmitChanges() updates Count() in one way but not in the other, see my comment in the code below.
(DC is the DataContext)

  var compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);

  if (compliances.Count() == 0) // Insert if not exists
  {
    DC.DataCompliances.InsertOnSubmit(new DataCompliance {
      FKCompany = c.Id,
      FKComplianceCriteria = criteria.Id
    });
    DC.SubmitChanges();

    compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);

    // At this point DC.DataCompliances.Count() has increased,
    // but compliances.Count() is still 0
    // When I refresh the page however, it will be 1
  }

Why does that happen?

I need to update compliances after inserting one. Does anyone have a solution?

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ