user1949387
user1949387

Reputation: 1285

mvc3 use entity to update many database records

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?

Upvotes: 0

Views: 575

Answers (2)

tam tam
tam tam

Reputation: 1900

 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();

Upvotes: 0

Nick Butler
Nick Butler

Reputation: 24433

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.

Upvotes: 2

Related Questions