首页 新闻 会员 周边

Linq 结果集作为多表查询条件

0
[待解决问题]

var queryOrg = nContext.Organizations.Where(a => a.Level == "1").Where(a => !string.IsNullOrEmpty(a.Name));

if (queryOrg.Count() > 0)
{
var queryP = (from l in nContext.BALogins
join b in nContext.Users on l.UserCode.ToString() equals b.Name.ToString()
join d in nContext.Organizations on b.OrganizationIds.Substring(37, 38) equals d.Id.ToString()
join p in queryOrg on d.ParentId.ToString() equals p.Id.ToString()
where (!string.IsNullOrEmpty(d.Name))
group l by new { p.Id, p.Name } into g
select new
{
Id = g.Key.Id,
value = g.Count(),
name = g.Key.Name,
}).ToList();
foreach (var s in queryP)
{
list.Add(new PieModel(s.value, s.name, s.Id));
}
return list;
}

第一个linq查询结果集作为第二个查询条件,这样是可以查询的

下面这个是自循环方法(给出一个部门ID,找出下面所有子部门)

public List<Organizations> GetChildDataCircle(Guid? pId)
{
List<Organizations> types = nContext.Organizations.Where(a => a.ParentId == pId).Where(a => !string.IsNullOrEmpty(a.Name)).OrderBy(b => b.Sequence).ToList();
children.AddRange(types);
foreach (var item in types)
{
GetChildDataCircle(item.Id);
}
return children;
}在另一个方法里面调用这个方法

List<Organizations> ss = GetChildDataCircle(pId);
ss.Add(new OrganizationsBLL().GetById(pId));
var sss = ss.AsQueryable();

var queryP = from l in nContext.BALogins
join b in nContext.Users on l.UserCode.ToString() equals b.Name.ToString()
from d in sss
where (!string.IsNullOrEmpty(d.Name)) && b.OrganizationIds.ToUpper().Substring(37, 38).Contains(d.Id.ToString())
group l by new { d.Id, d.Name } into g
select new
{
value = g.Count(),
name = g.Key.Name,
Id = g.Key.Id
};

就会报错,

Unable to create a constant value of type 'BizView.Model.Organizations'. Only primitive types or enumeration types are supported in this context.

说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NotSupportedException: Unable to create a constant value of type 'BizView.Model.Organizations'. Only primitive types or enumeration types are supported in this context.

源错误:

行 265:
行 266:        
行 267:                var query7P = (from l in nContext.BALogins
行 268:                              join b in nContext.Users on l.UserCode.ToString() equals b.Name.ToString()
行 269:                              join d in nContext.Organizations on b.OrganizationIds.Substring(37, 38) equals d.Id.ToString()


源文件: C:\Users\Lucifer\Desktop\BizView SVN\BizView\BizView.BLL\OrganizationsBLL.cs    行: 267

Aristotle的主页 Aristotle | 菜鸟二级 | 园豆:202
提问于:2016-05-09 20:45
< >
分享
所有回答(1)
0

from join 后这里先返回一个结果,然后从这个结果里再去查询试试
例如(from .. join ..).AsEnumerable();然后再查询

大杯美式不加糖不加奶 | 园豆:994 (小虾三级) | 2016-05-10 08:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册