Asp.net mvc 3 mvc3如何在收费表中包含计数方法

Asp.net mvc 3 mvc3如何在收费表中包含计数方法,asp.net-mvc-3,count,tolist,Asp.net Mvc 3,Count,Tolist,我有一个tolist方法,可以在数据库中进行更改,但我想计算我的Where子句为true的次数。收费表起作用我怎样才能加上一个计数 // the count to see how many times getid== x.RegistrationID List<Article> getprofile = (from x in db.Articles where getid == x.RegistrationID select x).ToList();

我有一个tolist方法,可以在数据库中进行更改,但我想计算我的Where子句为true的次数。收费表起作用我怎样才能加上一个计数

   // the count to see how many times getid== x.RegistrationID 
List<Article> getprofile = (from x in db.Articles where getid == x.RegistrationID select x).ToList();
            foreach (var items in getprofile)
            {
                items.articlecount = items.articlecount + 1;
                db.Entry(items).State = EntityState.Modified;
                db.SaveChanges();
            }
//查看getid==x.RegistrationID的次数的计数
列出getprofile=(从db.Articles中的x开始,其中getid==x.RegistrationID选择x);
foreach(getprofile中的var项)
{
items.articlecount=items.articlecount+1;
db.Entry(items).State=EntityState.Modified;
db.SaveChanges();
}

您似乎将文章数存储在db.Article中。为什么不存储在db.User中呢。如果存储在db.Article中,将很困难,因为它必须更改每个用户文章中的Article.articlecount。我认为上面的代码可能会导致不确定性计算。
每次在AddArticle metode中添加文章时,都会添加文章计数

public void AddArticle(Article article)
    {
        ... your add new article code

        // increase articlecount at db.User
        db.Users.Where(c => c.userid == article.getid).FirstOrDefault().articlecount += 1;
        db.SubmitChanges();
    }
或者不需要存储,只需获取计数:

int articlecount = db.Articles.Where(c => c.getid == RegistrationID ).Count();

我认为这样更安全。

似乎getprofile.count会给你这个答案。但是我怀疑如果它是那么简单,你就不需要发布问题了。所以请进一步澄清。getprofile.count在foreach上不起作用。。基本上,“articlecount”是数据库中的一个INT字段,它的唯一目的是统计用户创建了多少篇文章,当您创建一篇新文章时,articlecount应该增加+1。我使用.tolist和foreach更新使GetProfile为True的所有文章的“articlecount”。