我有以下代码 我有3个表作为Department,DepartmentTask和DepartmentPosition。以下是表格:
public partial class Department()
{
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
public Nullable<int> ParentDepartmentId { get; set; }
}
public partial class DepartmentPosition
{
public int PositionId { get; set; }
public int DepartmentId { get; set; }
public string PositiontName { get; set; }
}
public partial class DepartmentTask
{
public int TaskId { get; set; }
public int DepartmentId { get; set; }
public string TaskName { get; set; }
}
因此,任何部门都可以成为其他部门(任务和职位)的父母。所以在这里我想根据DepartmentId从表中选择所有记录,这些记录是特定部门的子项。
var z = from n in dbbs.Department
select new
{
deptcount = dbbs.Department.Count(p => p.ParentDepartmentId == id),
poscount = dbbs.DepartmentPosition.Count(x => x.DepartmentId == id),
taskcount = dbbs.DepartmentTask.Count(t => t.DepartmentId == id)
};
if(z.Count()>0)
{
//do something
}
else
{
}
现在如果表格中没有记录,那么它就会进入if块。我想知道如果根据查询没有记录,如何限制。
答案 0 :(得分:0)
我不确定你要用select语句实现什么。你不能用:
var deptcount = dbbs.Department.Count(p => p.ParentDepartmentId == id);
var poscount = dbbs.DepartmentPosition.Count(x => x.DepartmentId == id);
var taskcount = dbbs.DepartmentPosition.Count(t => t.DepartmentId == id);
if(deptcount > 0)
{
//do something
}
如果您尝试根据部门进行分组,请解释,我会更新我的答案。
答案 1 :(得分:0)
var count= from s in ObjectContext.tb1
join c in ObjectContext.tb2 on s.ParentCategoryID equals c.PersonCategoryID
join p in ObjectContext.tb3 on c.BusinessEntityID equals p.BusinessEntityID
select new count()
{
tbl1count =c.count()
tbl2count =p.count()
tbl3count =p.count()
};
然后
if(count.tbl1count.any()){
//do whatever you need}