在Asp .net Web API中使用交叉连接获取错误

时间:2013-09-23 17:50:10

标签: mysql asp.net linq web-services

您好我正在使用带有mysql数据库的Asp .net Web API中的交叉连接并收到以下错误:

错误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;

    }

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

1 个答案:

答案 0 :(得分:0)

您的查询(组合)返回匿名类型,您的方法签名表示您返回IQueryable<comment>。您无法从方法返回匿名类型,因此您有两个选项:

选项1:仅从“注释”表中选择要返回的字段。

选项2:创建一个包含评论和图片详细信息详细信息的新类,并修改您的查询以选择新的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.
}