2013-09-23 32 views
0

嗨,我使用跨在ASP .NET的Web API加入通過與一個MySQL數據庫並獲得以下錯誤:使用交叉連接在ASP .NET的Web API收到錯誤

錯誤1無法隱式轉換類型「系統.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; 

    } 

爲什麼會出現這個錯誤?任何幫助?

回答

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. 
} 
+0

我使用的數據庫第一種方法,有兩個單獨的列徵求意見和picturedetails 。所以在使用選項2之後,我的數據庫中這兩個表的數據是否會被提取? – Obvious

+0

數據仍將被提取。您只需將其存儲在數據傳輸對象(DTO)中,該數據傳輸對象便於邏輯(在業務邏輯意義上)組成數據。 –

+0

謝謝,會嘗試。 – Obvious