0

嗨,我在 Asp .net Web API 中通过 mysql 数据库使用交叉连接并收到以下错误:

错误 1 ​​无法将类型“System.Linq.IQueryable”隐式转换为“System.Linq.IQueryable”。存在显式转换(您是否缺少演员表?)

这是我的控制器代码

 private myappEntities db = new myappEntities();
    public IQueryable<comment>GetPicturesandtheirCommnets()
    {
        var combo=from p in db.picturedetails 
                    from c in db.comments

                    select new
                    {
                        p.iduser,p.idpictures,p.likes,p.nuditylevel,p.picTitle,p.pictime,p.fakeslevel,
                        c.comment1,c.ctime,c.idcomments,c.spamlevel,c.targetpictureid

                    };
        return combo;

    }

为什么我会收到此错误?有什么帮助吗?

4

1 回答 1

0

您的查询(组合)返回一个匿名类型,并且您的方法签名说您正在返回一个IQueryable<comment>. 您不能从方法返回匿名类型,因此您有两种选择:

选项 1:仅从 Comment 表中选择要返回的字段。

选项 2:创建一个包含来自 Comments 和 PictureDetails 的详细信息的新类,并修改您的查询以选择新的CommentAndPictureDetails(或您为类命名的任何名称)。

修改后的查询如下所示:

var combo=from p in db.picturedetails 
                    from c in db.comments

                    select new CommentAndPictureDetails
                    {
                        IdUser = p.iduser,
                        IdPictures = p.idpictures,
                        Likes = p.likes,
                        NudityLevel = p.nuditylevel,
                        PicTitle = p.picTitle,
                        PicTime = p.pictime,
                        FakesLevel = p.fakeslevel,
                        Comment1 c.comment1,
                        CTime = c.ctime,
                        IdComments = c.idcomments,
                        SpamLevel = c.spamlevel,
                        TargetPictureId = c.targetpictureid
                    };

你的类声明CommentAndPictureDetails是这样的:

public class CommentAndPictureDetails
{
    public string IdUser {get; set;}
    // I don't know the data types, so you'll have to make sure 
    // the .NET type matches the DB type.
}
于 2013-09-23T18:07:21.227 回答