简体   繁体   English

Linq如何返回匿名类型的IQueryable以获得其记录数?

[英]How does Linq return an anonymous type of IQueryable to get its record count?

The following Linq query returns an IQueryable of anonymous type: 以下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
         };

To get the number of records for users2, use the following method: 若要获取用户2的记录数,请使用以下方法:

var c = users2.Count();

Error in result: 结果错误:

Dbcomparonexpression requires parameters of a comparable type. Dbcomparonexpression需要可比较类型的参数。

How does an IQueryable type like this anonymous type get its record count? 像这样的匿名类型的IQueryable类型如何获得其记录计数?

Complete source code: 完整的源代码:

users: 用户:

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 };

distribs: 发行商:

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: 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();

In this code Still report the same error: 在此代码中仍然报告相同的错误:

var users2 = (from u1 in Users

The error DbComparisonExpression requires parameters of a comparable type likely relates to your use of .Equals() in where u.Equals("user") . 错误DbComparisonExpression requires parameters of a comparable type可能涉及您使用的.Equals()where u.Equals("user") It looks like u is a class, and you are comparing it to a string. 它看起来像u是一个类,而你比较它与一个字符串。 You are comparing two different types. 您正在比较两种不同的类型。

对Linq查询结果使用ToList转换,sub linq也将被转换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM