0

I am trying to use entity to update many records up to 30 if the conditions inside the statement match but i am having an issue finding the correct method to use at the end what I have is this

public ActionResult Edit(profile profile)
{
  //change all records where getid== x.registrationID
  var getprofile =
    (from x in db.Articles where getid == x.RegistrationID select x).Any();

  getprofile.firstname = profile.firstname;
  getprofile.lastname = profile.lastname;
}

The error i get is on getprofile.firstname and getprofile.lastname saying the bool does not contain a definition for firstname or lastname. If i put in FirstorDefault() everything works fine but of course it only changes the 1st record...

How can I change many records?

2 Answers 2

2

You can use ToList() to get a collection of Articles:

List<Article> getprofiles = ( from x in db.Articles ... ).ToList();

foreach( Article getprofile in getprofiles )
{
  getprofile.firstname = profile.firstname;
  getprofile.lastname = profile.lastname;
}

db.SaveChanges();

This queries the database, gets the matching Article rows and puts them in a collection - a List<Article>

You can then modify the objects in the collection and finally call db.SaveChanges() to update the database.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, .any() returns bool so you cant edit the result. it tells you there were matching records yes or no. Get a list of matching records, edit the list and save result.
0
 var objRD = objBS.Articles.Where(c => c.RegistratinID.Equals(getID));

        if (objRD.Count() > 0)
        {
            foreach (ReportingData objR in objRD)
            {
                objR.firstname = profile.firstname;
                objR.lastname = profile.lastname;

            }
        }
        objBS.SaveChanges();

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.