我正在研究MVC3项目。我需要帮助写Linq表达式。我的模型是这样的。
模型类
public int Id { get; set; }
public int DetCount { get; set; }
我需要做这样的事情,
DetCount = (from sel in db.PoDetails where sel.PoId == Id select sel).Count(); // Id is current model Id.
根据父表Id,我需要获取子表记录计数。
示例
父表
Id Name
1 XYZ
2 ABC
儿童表
Id P_Id
1 1
2 1
3 2
DetCount = (from sel in db.child where sel.P_Id == Id select sel).Count(); // if Id= 1
结果
DetCount = 2;
我写的代码是这样的。
代码
model = ...
select new porders
{
Id = p.Id, //This Id is passed to next statement for DetCount.
DetCount = (from sel in db.PoDetails where sel.PoId == Id select sel).Count(); // I need to pass value from another linq query.
}
请帮助我。
谢谢,
答案 0 :(得分:0)
var result = select new porders
{
Id = p.Id,
PODate = p.Date.Value,
RefNo = p.RefNo,
Status = resloc.Description,
Supplier = resstat.Name,
DetCount = db.PoDetails
.Select(p => p.sel)
.Where(sel.PoId == asd.Id).Count()
}
答案 1 :(得分:0)
这只是你想要的数量吗?如果是这样,这应该有效:
var DetCount = db.PoDetails.Where(sel=>sel.PoId == Id).Count(); // Id is current model Id.
干杯:)