码迷,mamicode.com
首页 > 其他好文 > 详细

EF中另外一個解決類似連表的方法

时间:2014-09-15 17:17:29      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:io   ar   for   文件   sp   cti   on   c   new   

1.首先我們想讓列表頁顯示兩個表的共同數據

這裡有兩張表

   public class mytype
    {
        public int mytypeID { get; set; }
        public string mytypeName { get; set; }
    }

  public class Author
    {
        public int AuthorID { get; set; }
        public string AuthorName { get; set; }
    }

2.然後新建立一個新的文件夾,然後添加一個類

  public class all
    {
        public int id { get; set; }

        public string type { get; set; }

        public string other { get; set; }

    }

3.  public class mytryController : Controller
    {
        //
        // GET: /mytry/
        AllContext db = new AllContext();

        public ActionResult Index() //在這裡我們獲取到了類all中的數據
        {
            var a = from i in db.Mytype
                    from j in db.Author
                    where i.mytypeID == j.AuthorID
                    select new all{ id=i.mytypeID, type=i.mytypeName, other=j.AuthorName };
            return View(a);
        }
    }

4.然後再對應的視圖頁面

@model IEnumerable<MvcApplication12.viewModel.all>

然後再下面下出,這樣你所需要的數據就出來了

  @foreach (var item in Model)
    {
     @item.id
     @item.type
     @item.other
     }

EF中另外一個解決類似連表的方法

标签:io   ar   for   文件   sp   cti   on   c   new   

原文地址:http://www.cnblogs.com/sdya/p/3972968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!