0

Suppose that i have

public IQueryable<Application> GetApplications() {
    var applications = _context.Applications.Where(v => v.Exported == false);
    // Now i want to flag exported = true because i am exporting them
    foreach (var application in applications){
        _context.Applications.SingleOrDefault(v => v.ApplicationId == application .ApplicationId).Exported = true;
}
    _context.SaveChanges();
    return applications;
}

I am not getting any applications because _context.SaveChanges(); ( i suppose) changed the applications variable. How can i change the Database without changing my local variable.

2
  • Well that seems very weird and should not happen. Make sure that applications is not empty collection at first line of method. Commented Jul 2, 2015 at 11:34
  • its not empty. while debugging i can see the applications variable populated and after SaveChanges applications is empty. Commented Jul 2, 2015 at 11:38

1 Answer 1

1

Call ToList to execute query before changing items:

var applications = _context.Applications.Where(v => v.Exported == false).ToList();
Sign up to request clarification or add additional context in comments.

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.