Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# mvc3使用实体更新许多数据库记录_C#_Asp.net Mvc 3_Entity Framework_Updates - Fatal编程技术网

C# mvc3使用实体更新许多数据库记录

C# mvc3使用实体更新许多数据库记录,c#,asp.net-mvc-3,entity-framework,updates,C#,Asp.net Mvc 3,Entity Framework,Updates,如果语句中的条件匹配,我尝试使用entity来更新多达30条记录,但我遇到了一个问题,那就是如何在最后找到正确的方法 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()

如果语句中的条件匹配,我尝试使用entity来更新多达30条记录,但我遇到了一个问题,那就是如何在最后找到正确的方法

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;
}
我得到的错误是在
getprofile.firstname
getprofile.lastname
上说
bool
不包含
firstname
lastname
的定义。如果我输入
FirstorDefault()
一切正常,但当然它只会更改第一条记录

如何更改多个记录?

您可以使用
ToList()
获取
文章的集合

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

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

db.SaveChanges();
List getprofiles=(从db.Articles中的x开始….ToList();
foreach(getprofiles中的文章getprofile)
{
getprofile.firstname=profile.firstname;
getprofile.lastname=profile.lastname;
}
db.SaveChanges();
这将查询数据库,获取匹配的
文章
行,并将它们放入一个集合-一个
列表


然后,您可以修改集合中的对象,并最终调用
db.SaveChanges()
来更新数据库。

+1,.any()返回bool,因此无法编辑结果。它告诉你有匹配的记录是或否。获取匹配记录的列表,编辑列表并保存结果。谢谢你们,这真的很有帮助。
 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();