使用IQueryable< bool>编译查询返回值 [英] Compiling query with IQueryable<bool> return value

查看:99
本文介绍了使用IQueryable< bool>编译查询返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个职位ID列表,我试图返回每个帖子的当前用户的投票状态(如果该用户对该职位进行了投票,则为true;否则,则为false).每件事对我来说似乎都不错,但我遇到两个错误.

I have a list of Post Ids and I'm trying to return vote status of the current user for each post (true if the user has voted for that post and false if he has not). Every thing seems ok to me but I'm getting two errors.

public static Func<DatabaseDataContext, string, int[], IQueryable<bool>>
    GetUserVoted = CompiledQuery.Compile((DatabaseDataContext db, string UserId, int[] PostIds)
    => (from p in PostIds
        select new 
        {
           db.Votes.Any(v=> v.PostId==p && v.UserId == UserId)
        })
    );

我遇到2个错误:

无效的匿名类型成员声明符.必须使用成员分配,简单名称或成员访问权限来声明匿名类型成员.

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

还有

不能隐式转换类型'System.Func< DatabaseDataContext,string,int [],System.Collections.Generic.IEnumerable>'到'System.Func< DatabaseDataContext,string,int [],System.Linq.IQueryable>'.存在显式转换(您是否缺少演员表?)

Cannot implicitly convert type 'System.Func< DatabaseDataContext,string,int[],System.Collections.Generic.IEnumerable>' to 'System.Func< DatabaseDataContext,string,int[],System.Linq.IQueryable>'. An explicit conversion exists (are you missing a cast?)

推荐答案

如果要使用匿名类型,则其成员应具有名称.您缺少HasAnyVote这样的名称:

If you are going to use Anonymous Type, then its members should have a name. You're missing a name like HasAnyVote:

 select new 
        {
           HasAnyVote = db.Votes.Any(v=> v.PostId==p && v.UserId == UserId)
        })

或直接使用它:

(from p in PostIds
select db.Votes.Any(v=> v.PostId==p && v.UserId == UserId))
.AsQueryable<bool>()

这篇关于使用IQueryable&lt; bool&gt;编译查询返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆