以下Linq查询返回匿名类型的IQueryable:
var users2 = from u1 in Users
join u2 in distribs on u1.pDistribId equals u2.Id
into temp
from u3 in temp.DefaultIfEmpty()
select new {
u1.Id,
u1.UserName,
u1.pDistribId,
pUserName = u3 == null ? "" : u3.UserName,
u1.Phone,
u1.Name,
u1.pCustomUser,
u1.CoName,
u1.CoPhone,
u1.Birthday,
u1.QQ,
u1.Email,
u1.SelfIntr
};
要获取用户2的记录数,请使用以下方法:
var c = users2.Count();
结果错误:
Dbcomparonexpression需要类似类型的参数。
像这样的匿名类型的IQueryable类型如何获得其记录计数?
完整的源代码:
用户:
var Users = from u in UserManager.Users
where u.Equals("user")
select new { u.Id, u.UserName, u.pDistribId, u.Phone, u.Name, u.CoName, u.CoPhone, u.pCustomUser, u.Birthday, u.QQ, u.Email, u.SelfIntr };
散列:
var distribs = from u in UserManager.Users
where u.Role.Equals("distrib")
select new { u.Id, u.UserName, u.pDistribId, u.Phone, u.Name, u.CoName, u.CoPhone, u.pCustomUser, u.Birthday, u.QQ, u.Email, u.SelfIntr };
users2:
var users2 = (from u1 in Users
join u2 in distribs on u1.pDistribId equals u2.Id
into temp
from u3 in temp.DefaultIfEmpty()
select new {
u1,
u3
}).ToList()
.Select(x => new {
x.u1.Id,
x.u1.UserName,
x.u1.pDistribId,
pUserName = x.u3 == null ? "" : x.u3.UserName,
x.u1.Phone,
x.u1.Name,
x.u1.pCustomUser,
x.u1.CoName,
x.u1.CoPhone,
x.u1.Birthday,
x.u1.QQ,
x.u1.Email,
x.u1.SelfIntr
});
var c = users2.Count();
在此代码中仍报告相同的错误:
var users2 = (from u1 in Users
答案 0 :(得分:0)
错误DbComparisonExpression requires parameters of a comparable type
可能与您在.Equals()
中使用where u.Equals("user")
有关。看起来u
是一个类,您正在将其与字符串进行比较。您正在比较两种不同的类型。
答案 1 :(得分:0)
对Linq查询结果使用ToList转换,sub linq也将被转换