【发布时间】:2013-12-12 19:55:41
【问题描述】:
我有三个对象:
public class Part
{
[Key]
public int ID { get; set; }
public string Number { get; set; }
public string Name { get; set; }
public int descID { get; set; }
}
public class Description
{
[Key]
public int descID { get; set; }
public string descName { get; set; }
public string description { get; set; }
}
public class GridPart
{
public string name{ get; set; }
public string number { get; set; }
public string description { get; set; }
}
我正在使用LINQ 在 descID 列上加入 Part 和 Description:
public ActionResult Index()
{
var myParts = from p in db.Parts
join d in db.Description on p.descID equals d.DescriptorID
select new { Description = d.description, Name = p.name};
List<GridPart> partsList = new List<GridPart>();
foreach (var m in myParts)
{
GridPart gp = new GridPart();
gp.Name = m.name;
gp.description = m.description;
partsList.Add(gp);
}
return View(partsList);
}
如果我只是使用 Parts 表,在视图中我会这样做:
@model IEnumerable<MyApp.Models.Part>
如果我正在使用连接表,我该怎么办?这同时使用了 Parts 和 Description,更不用说我的 GridParts 列表,我如何通过它来显示我需要的所有数据?
非常感谢任何建议。
【问题讨论】:
标签: asp.net-mvc linq entity-framework