一天的美好時光!Linq to Entity |通過表加入模型
我在控制器查詢MVC項目:
var getPhotos = (from m in db.photos
join n in db.comments on m.id equals n.photoid
where n.ownerName == User.Identity.Name
orderby n.id descending
select new {
m.imgcrop, m.id,
n.commenterName, n.comment
}).Take(10);
如何通過這個查詢,查看模型,該模型來查看。
花費整晚找到的例子,但不能。感謝幫助!
修訂 全部模型類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace photostorage.Models
{
public class GlobalModel
{
public class PhotoViewModel
{
public photos Photos { get; set; }
public profiles Profile { get; set; }
public IQueryable<comments> Comments { get; set; }
public IQueryable<photos> NextPrev { get; set; }
}
public class UserPhotoList
{
public IQueryable<photos> Photos { get; set; }
public profiles Profile { get; set; }
}
public class UserProfileView
{
public IQueryable<photos> Photos { get; set; }
public profiles Profile { get; set; }
}
public class GetLastComments
{
public IQueryable<photos> uPhoto { get; set; }
public IQueryable<comments> uComments { get; set; }
}
}
}
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using photostorage.Models;
namespace photostorage.Controllers
{
public class HomeController : Controller
{
private photostorageEntities db = new photostorageEntities();
public ActionResult Index()
{
if(Request.IsAuthenticated) {
GlobalModel.GetLastComments model = new GlobalModel.GetLastComments();
var getPhotos = (from m in db.photos
join n in db.comments on m.id equals n.photoid
where n.ownerName == User.Identity.Name
select new {
m.imgcrop, m.id,
n.commenterName, n.comment
}).Take(10);
return View("Index_Auth", model);
}else{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View("Index");
}
}
public ActionResult About()
{
return View();
}
}
}
您如何期待我們回答此問題而不顯示您的視圖模型的外觀或至少解釋您的視圖需要展示什麼以及這與您使用EF的模型有何不同?至於傳遞視圖模型到視圖,我認爲這應該是非常簡單的:'返回視圖(someInstanceOfAViewModel);'。 – 2011-05-12 21:45:13
如何將此查詢轉換爲返回View(someInstanceOfAViewModel);如果現在我像你寫的一樣,我有一個錯誤。 PIf對你來說不難,只是寫了一個模型和控制器的小例子。 – 2011-05-12 21:50:02
我認爲你編輯了這個模型和控制器,但它仍然看起來不完整。當然,Craig M在下面通過創建一個新模型回答了這個問題,但是上面的代碼引用了m.imgcrop,m.id,n.commenterName和n.comment,它們不是上述任何模型的一部分,所以很難知道你從哪裏提取數據(表格,視圖等)。 db.Photos和db.Comments模型丟失。上面的這些模型並沒有告訴我你想做什麼。 – vapcguy 2014-08-09 00:41:44