【问题标题】:Using Cross join in Asp .net Web API getting error在 Asp .net Web API 中使用交叉连接出现错误
【发布时间】:2013-09-28 17:28:35
【问题描述】:

您好,我在 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;

    }

为什么会出现这个错误??有什么帮助吗?

【问题讨论】:

    标签: mysql asp.net linq web-services


    【解决方案1】:

    您的查询(组合)返回一个匿名类型,并且您的方法签名表明您正在返回一个IQueryable&lt;comment&gt;。您不能从方法中返回匿名类型,因此您有两种选择:

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

    选项 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.
    }
    

    【讨论】:

    • 我使用的是数据库优先方法,cmets 和图片详细信息有两个单独的列。那么在使用选项 2 之后,我的数据库中这两个表中的数据会被获取吗?
    • 仍然会获取数据。您只是将其存储在数据传输对象 (DTO) 中,这有助于数据的逻辑(在业务逻辑意义上)组合。
    • 无法将类型“System.Linq.IQueryable”隐式转换为“System.Linq.IQueryable”。存在显式转换(您是否缺少演员表?)
    猜你喜欢
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 2021-11-30
    • 2013-10-06
    • 2013-01-20
    • 1970-01-01
    相关资源
    最近更新 更多