0

我在 mvc3 中有数据库第一个应用程序。有 Rating 表,它有整数类型的IDValue属性。计数始终为 5。我想输入新值,将其设置为 Ratings 的最后一个元素,并且每个元素的值都传递到元素的值之前。价值观从最后一个滑到第一个。

public ActionResult UpdateRating( int newValue )
{

var rating0 = db.Ratings.ElementAt( 0 );
var rating1 = db.Ratings.ElementAt( 1 );
var rating2 = db.Ratings.ElementAt( 2 );
var rating3 = db.Ratings.ElementAt( 3 );
var rating4 = db.Ratings.ElementAt( 4 );

rating0.Value = rating1.Value;
rating1.Value = rating2.Value;
rating2.Value = rating3.Value;
rating3.Value = rating4.Value;
rating4.Value = newValue ;

db.Ratings.Attach( rating0 );
db.ObjectStateManager.ChangeObjectState( rating0 , EntityState.Modified );
db.Ratings.Attach( rating1 );
db.ObjectStateManager.ChangeObjectState( rating1 , EntityState.Modified );
db.Ratings.Attach( rating2 );
db.ObjectStateManager.ChangeObjectState( rating2 , EntityState.Modified );
db.Ratings.Attach( rating3 );
db.ObjectStateManager.ChangeObjectState( rating3 , EntityState.Modified );
db.Ratings.Attach( rating4 );
db.ObjectStateManager.ChangeObjectState( rating4 , EntityState.Modified );
db.SaveChanges();
...
}

我收到错误 db.Ratings.ElementAt(); 线。我该怎么做这个操作?

4

1 回答 1

2

用。。。来代替

public ActionResult UpdateRating( int newValue )
{

List<Rating> ratings = db.Ratings.OrderBy(r => r.id)
                        .ToList();

ratings[0].Value = ratings[1].Value;
ratings[1].Value = ratings[2].Value;
ratings[2].Value = ratings[3].Value;
ratings[3].Value = ratings[4].Value;
ratings[4].Value = newValue;

db.SaveChanges();
}
于 2012-09-03T06:55:30.213 回答