C# 在foreach中调用返回Int32的方法
我希望您能帮助我理解MVC5中的以下内容。我已经读了很多以前的帖子,但我没有得到我需要的答案 我有一个商店,里面有很多商品,但是商品可以是不同的类型。我想在Store/Index视图中显示一个包含所有存储的表,其中包含每个存储及其不同项目类型的计数 e、 g.(布局只是为了简单明了) 商店------A------B------C 桤木------10------22------43 Birmh------07------11------89 等等 除了商店和商品模型,我还有一个StoreDetails视图模型,其中包含Storename和counts 在Store/Index controller中,我构建了一个StoreDetails列表,以传递给Store/Index视图 要计算计数,我在存储/索引控制器中执行以下操作:C# 在foreach中调用返回Int32的方法,c#,asp.net-mvc-5,C#,Asp.net Mvc 5,我希望您能帮助我理解MVC5中的以下内容。我已经读了很多以前的帖子,但我没有得到我需要的答案 我有一个商店,里面有很多商品,但是商品可以是不同的类型。我想在Store/Index视图中显示一个包含所有存储的表,其中包含每个存储及其不同项目类型的计数 e、 g.(布局只是为了简单明了) 商店------A------B------C 桤木------10------22------43 Birmh------07------11------89 等等 除了商店和商品模型,我还有一个StoreDet
public ActionResult Index()
{
var model = db.Stores
.OrderBy(s => s.StoreName)
.Select(s => new StoreDetails
{
ID = s.ID,
Name = s.StoreName,
CountOfItems = s.Items.Count(),
CountOfBigType = BigCount(s.ID)
//other counts if i can get this one working
});
return View(model);
}
我的BigCount方法是:
public int BigCount(int storeid)
{
var big = from t in db.Items
where t.StoreID == storeID && t.Size > 100
select t;
return big.Count();
}
当CountOfBigType()未显示时,CountOfItems在视图中正确显示,但添加后,我会得到Linq to Entities错误
有什么建议吗
问候
Craig您是否不能简单地修改初始LINQ查询,如下所示并获得相同的结果:
public ActionResult Index()
{
var model = db.Stores.OrderBy(s => s.StoreName)
.Select (s => new StoreDetails {
ID = s.ID,
Name = s.StoreName,
CountOfItems = s.Items.Count(),
CountOfBigType = s.Items.Count(x=>x.Size > 100) <--
//other counts if i can get this one working
});
return View(model);
}
public ActionResult Index()
{
var model=db.Stores.OrderBy(s=>s.StoreName)
。选择(s=>newstoredetails){
ID=s.ID,
Name=s.StoreName,
CountOfItems=s.Items.Count(),
CountOfBigType=s.Items.Count(x=>x.Size>100)您是否不能简单地修改初始LINQ查询,如下所示并获得相同的结果:
public ActionResult Index()
{
var model = db.Stores.OrderBy(s => s.StoreName)
.Select (s => new StoreDetails {
ID = s.ID,
Name = s.StoreName,
CountOfItems = s.Items.Count(),
CountOfBigType = s.Items.Count(x=>x.Size > 100) <--
//other counts if i can get this one working
});
return View(model);
}
public ActionResult Index()
{
var model=db.Stores.OrderBy(s=>s.StoreName)
。选择(s=>newstoredetails){
ID=s.ID,
Name=s.StoreName,
CountOfItems=s.Items.Count(),
CountOfBigType=s.Items.Count(x=>x.Size>100)您是否不能简单地修改初始LINQ查询,如下所示并获得相同的结果:
public ActionResult Index()
{
var model = db.Stores.OrderBy(s => s.StoreName)
.Select (s => new StoreDetails {
ID = s.ID,
Name = s.StoreName,
CountOfItems = s.Items.Count(),
CountOfBigType = s.Items.Count(x=>x.Size > 100) <--
//other counts if i can get this one working
});
return View(model);
}
public ActionResult Index()
{
var model=db.Stores.OrderBy(s=>s.StoreName)
。选择(s=>newstoredetails){
ID=s.ID,
Name=s.StoreName,
CountOfItems=s.Items.Count(),
CountOfBigType=s.Items.Count(x=>x.Size>100)您是否不能简单地修改初始LINQ查询,如下所示并获得相同的结果:
public ActionResult Index()
{
var model = db.Stores.OrderBy(s => s.StoreName)
.Select (s => new StoreDetails {
ID = s.ID,
Name = s.StoreName,
CountOfItems = s.Items.Count(),
CountOfBigType = s.Items.Count(x=>x.Size > 100) <--
//other counts if i can get this one working
});
return View(model);
}
public ActionResult Index()
{
var model=db.Stores.OrderBy(s=>s.StoreName)
。选择(s=>newstoredetails){
ID=s.ID,
Name=s.StoreName,
CountOfItems=s.Items.Count(),
CountOfBigType=s.Items.Count(x=>x.Size>100)完美。我不能呕吐,因为我是一个低“1”!!如果我可以的话我会的。谢谢。没问题,很高兴我能帮上忙。@CraigRoberts不过你可以把这个作为公认的答案,你应该这么做。完美。我不能呕吐,因为我是一个低“1”!!如果可以的话,我会的。谢谢。没问题,很高兴我能帮上忙。@CraigRoberts不过,你可以让这成为公认的答案,你应该这样做。太好了。我不能呕吐,因为我是一个卑微的“1”!!如果我可以的话我会的。谢谢。没问题,很高兴我能帮上忙。@CraigRoberts你可以让这个被接受的答案,你应该这样做。很好。我不能呕吐,因为我是一个卑微的“1”!!如果我可以的话我会的。谢谢。没问题,很高兴我能帮上忙。@CraigRoberts你可以让这个被接受的答案,你应该这样做。