【发布时间】:2011-02-15 10:16:15
【问题描述】:
以下代码似乎没有得到正确的计数.....
var materials = consRepository.FindAllMaterials().AsQueryable();
int count = materials.Count();
这是这样做的方式吗....这是我获取记录的存储库...
public IQueryable<MaterialsObj> FindAllMaterials()
{
var materials = from m in db.Materials
join Mt in db.MeasurementTypes on m.MeasurementTypeId equals Mt.Id
where m.Is_Deleted == 0
select new MaterialsObj()
{
Id = Convert.ToInt64(m.Mat_id),
Mat_Name = m.Mat_Name,
Mes_Name = Mt.Name,
};
return materials;
}
编辑:
当我使用这个时,
var materials = consRepository.FindAllMaterials().AsQueryable();
return View("Materials", materials);
我的表中有 18 行...所以我不能将计数设为18,而是给出12
回答:
Breakpoint 似乎没有产生结果,但 response.Write(count) 产生了结果...
【问题讨论】:
-
你为什么要转换成
IQueryable?IQueryable<MaterialsObj>已经有一个Count()方法。您观察到预期计数和实际计数之间有什么差异? -
你怎么知道计数不正确?它应该返回什么而不是返回什么?
-
这不行,重新安装Windows。
标签: asp.net-mvc linq-to-sql count iqueryable