MVC3 LINQ加入 [英] MVC3 linq joins
问题描述
作为一个新手我试图在MVC3,剃须刀,EF我的手我有我想从产生它的景色三连表。在一个傻瓜的短暂以下是关于表
PJUsers - ID,memUID(从会员这个唯一的ID),名字,姓氏
PJAwards - 用户提名另一用户的一个奖项,以awardtypesID这个链接作为外键(awardId,BOOL:awardok)
PJAwartypes - (awardtypeID,awardName)
在控制器中的查询是这样的。
VAR名单=
从db.PJawards TL
加入ü在tl.nomineeId db.PJUsers等于u.ID到tl_u
加入我在tl.nominatorId db.PJUsers等于i.MemUID到tl_i
其中,tl.awardOk
排序依据tl.awardDated上升从u在tl_u.DefaultIfEmpty()
从我tl_i.DefaultIfEmpty()
新选择
{
状态= tl.awardOk,
nomineeFname = u.FirstName,
nomineeLname = u.LastName,
奖= tl.PJawards.awardName,
日= tl.awardDated,
nominatorFname = i.FirstName,
nominatorLname = i.LastName,
nomineeCountry = u.Citizen,
nomineeResidence = u.Residence,
awardtypeId = tl.ID
};
某处我读,我必须构建在控制器类似查询模型类
{
公共类AwardUserInfo
{
公共AwardUserInfo(){}
公共BOOL状态{搞定;组; }
公共字符串nomineeFname {搞定;组; }
公共字符串nomineeLname {搞定;组; }
公共字符串奖{搞定;组; }
公共字符串在{搞定;组; }
公共字符串nominatorFname {搞定;组; }
公共字符串nominatorLname {搞定;组; }
公共字符串nomineeCountry {搞定;组; }
公共字符串nomineeResidence {搞定;组; }
公众诠释awardtypeId {搞定;组; }}
}
请我学会这样的例子,能够帮助我以为我什么都不知道。
某处我读,我必须构建在控制器类似查询模型类
块引用>尝试这个。
我猜你的EF-模型类似于
所以,你可以创建一个视图模型类
公共类PJAwardsViewModel
{
公众诠释标识{搞定;组; }
公共字符串NominatorFName {搞定;组; }
公共字符串NomineeFname {搞定;组; }
公共字符串AwardName {搞定;组; }
公共BOOL IsAwarded {搞定;组; }
}如果你添加一些服务类这将是还不错
公共类PJAwardsService
{
公共静态列表< PJAwards>得到所有()
{
使用(VAR上下文=新YourDBEntities())
{
返回context.PJAwards
.INCLUDE(X => x.PJUsers)
.INCLUDE(X => x.PJUsers1)
.INCLUDE(X => x.PJAwartypes).ToList();
}
}
}(不要忘了写
使用System.Data.Entity的;
)然后,您可以添加一个ViewModelHelper类
公共类PJAwardsViewModelHelper
{
公共静态PJAwardsViewModel PopulatePJAwardsViewModel(PJAwards pjaward)
{
返回新PJAwardsViewModel
{
ID = pjaward.Id,
NominatorFName = pjaward.PJUsers.FirstName,
NomineeFname = pjaward.PJUsers1.FirstName,
AwardName = pjaward.PJAwartypes.AwardName,
IsAwarded = pjaward.IsAwarded
};
} 公共静态列表< PJAwardsViewModel> PopulatePJAwardsViewModelList(列表< PJAwards> pjawardsList)
{
返回pjawardsList.Select(X => PopulatePJAwardsViewModel(X))。了ToList();
}
}在结束您的控制器指数方法看起来像这样
公众的ActionResult指数()
{
变种pjawards = PJAwardsViewModelHelper.PopulatePJAwardsViewModelList(PJAwardsService.GetAll()了ToList());
返回查看(pjawards);
}你唯一应该做的就是添加一个视图(前建项目)。选择
PJAwardsViewModel
为Model类
和列表
为脚手架模板
。
享受它。As a novice am trying my hands on MVC3,razor, EF I have Three connected Tables that I want to produce a view from it. In a simpleton's brief the following are about the tables
PJUsers - ID, memUID(this unique Id from membership),FirstName,LastName
PJAwards - user nominates another user for an award, this links with awardtypesID as foreign key ( awardId,bool:awardok)
PJAwartypes - (awardtypeID, awardName)
The query in the controller is like this
var lists = from tl in db.PJawards join u in db.PJUsers on tl.nomineeId equals u.ID into tl_u join i in db.PJUsers on tl.nominatorId equals i.MemUID into tl_i where tl.awardOk orderby tl.awardDated ascending from u in tl_u.DefaultIfEmpty() from i in tl_i.DefaultIfEmpty() select new { Status = tl.awardOk, nomineeFname = u.FirstName, nomineeLname = u.LastName, award = tl.PJawards.awardName, Dated = tl.awardDated, nominatorFname = i.FirstName, nominatorLname = i.LastName, nomineeCountry = u.Citizen, nomineeResidence = u.Residence, awardtypeId = tl.ID };
somewhere i read that i have to construct a model class similar to the query in the controller
{ public class AwardUserInfo { public AwardUserInfo() { } public bool Status { get; set; } public string nomineeFname { get; set; } public string nomineeLname { get; set; } public string award { get; set; } public string Dated { get; set; } public string nominatorFname { get; set; } public string nominatorLname { get; set; } public string nomineeCountry { get; set; } public string nomineeResidence { get; set; } public int awardtypeId { get; set; } } }
Please I learn by examples so to be able to help me assume I don't know anything
解决方案somewhere i read that i have to construct a model class similar to the query in the controller
Try this.
I guess your ef-model is similar to
So You can create a ViewModel class
public class PJAwardsViewModel { public int Id { get; set; } public string NominatorFName { get; set; } public string NomineeFname { get; set; } public string AwardName { get; set; } public bool IsAwarded { get; set; } }
It will be also good if You add some service class
public class PJAwardsService { public static List<PJAwards> GetAll() { using (var context = new YourDBEntities()) { return context.PJAwards .Include(x => x.PJUsers) .Include(x => x.PJUsers1) .Include(x => x.PJAwartypes).ToList(); } } }
(Don't forget to write
using System.Data.Entity;
)Then You can add a ViewModelHelper class
public class PJAwardsViewModelHelper { public static PJAwardsViewModel PopulatePJAwardsViewModel(PJAwards pjaward) { return new PJAwardsViewModel { Id = pjaward.Id, NominatorFName = pjaward.PJUsers.FirstName, NomineeFname = pjaward.PJUsers1.FirstName, AwardName = pjaward.PJAwartypes.AwardName, IsAwarded = pjaward.IsAwarded }; } public static List<PJAwardsViewModel> PopulatePJAwardsViewModelList(List<PJAwards> pjawardsList) { return pjawardsList.Select(x => PopulatePJAwardsViewModel(x)).ToList(); } }
At the end Your controller index method will look like this
public ActionResult Index() { var pjawards = PJAwardsViewModelHelper.PopulatePJAwardsViewModelList(PJAwardsService.GetAll().ToList()); return View(pjawards); }
The only thing You should do is add a view (build the project before). Choose
PJAwardsViewModel
as aModel class
andList
as ascaffold template
. Enjoy it.这篇关于MVC3 LINQ加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!