zoukankan      html  css  js  c++  java
  • MVC EF 导航属性

    @model IQueryable<EFExam.Models.CategoryProductViewModel>
    @{
        Layout = null;
    }


    <!DOCTYPE html>


    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <div>
            <table>
                <tr>
                    <th>CategoryName</th>
                    <th>ProductName</th>
                </tr>
                @foreach (EFExam.Models.CategoryProductViewModel cp in Model)
                {
                    <tr>
                        <td>@cp.NCategoryName</td><td>@cp.NProductName</td>
                    </tr>
                }
            </table>
        </div>
    </body>
    </html>

    using EFExam.Models;
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;


    namespace EFExam.Controllers
    {
        public class CategoryController : Controller
        {
            //
            // GET: /Category/


            public ActionResult Index()
            {
                DbContext context = new StoreContext();
                //var list = from category in context.Set<Category>()
                //           join product in context.Set<Product>()
                //           on category.CategoryID equals product.CategoryID
                //           select category;


                //导航属性 第一种方法1对多 多from语句
                //var list = from category in context.Set<Category>()
                //           from product in category.Products
                //           select new CategoryProductViewModel
                //           {
                //               NCategoryName = category.CategoryName,
                //               NProductName = product.ModelName
                //           };


                //导航属性 第二种方法多对1 
                var list = from product in context.Set<Product>()
                           select new CategoryProductViewModel()
                           {
                               NCategoryName = product.Category.CategoryName,
                               NProductName = product.ModelName
                           };


                return View(list);
            }


        }
    }

  • 相关阅读:
    指令到底是什么?机器码又是什么?
    汇编基础最后一篇--机器语言指令
    剑指OFFER----面试题34. 二叉树中和为某一值的路径
    剑指OFFER----面试题33. 二叉搜索树的后序遍历序列
    剑指OFFER----面试题32
    剑指OFFER----面试题31. 栈的压入、弹出序列
    剑指OFFER----面试题30. 包含min函数的栈
    剑指OFFER----面试题29. 顺时针打印矩阵
    剑指OFFER----面试题28. 对称的二叉树
    剑指OFFER----面试题27. 二叉树的镜像
  • 原文地址:https://www.cnblogs.com/dxmfans/p/9434741.html
Copyright © 2011-2022 走看看