如何在MVC项目中使用Entity Framework 6和Linq合并2个表? [英] How to combine 2 tables using Entity Framework 6 and Linq in an MVC Project?

查看:40
本文介绍了如何在MVC项目中使用Entity Framework 6和Linq合并2个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从关系表中获取数据.

I want to know how to get data from relationals tables.

我想从与"Noticias"相关的名为"Noticias1"的表中获取图像(Noticias是西班牙语,表示对不起新闻,但这对我的大学而言).

I want to get the images from the table named "Noticias1" that is relationated with "Noticias" (Noticias is an spanish word that means News sorry but it is for my University).

此处图图像

这是我的" Noticias1 "表,该表获取将包含表"Noticias"中的新闻的图像

Here my "Noticias1" table that gets the images that will contain the news in table "Noticias"

这是我的" Noticia "表,其中仅包含1个"Noticia",这表示英文新闻

Here my "Noticia" Table that only contain 1 "Noticia" that means News in english

实际视图IMG

如您所见,它仅显示"Noticias"表,该表只有1条新闻,这不是问题.

As you can see it only shows "Noticias" Table that only have 1 News that is not the problem.

现在,我想将所有图像从"Noticias1"发送到新闻"中的每个新闻中 表格"Noticias"显示在我的视图中. (名称为1_0的将是 特色img).

Now I want to get the all the Images from "Noticias1" to every News in the table "Noticias" to show it in my view. (the named 1_0 will be the featured img).

这是我的控制器

public class NoticiasController : Controller
    {
        // GET: Noticias
        public ActionResult Index(int? page)
        {
            var entities = new Model.CobecaIntranetEntities();
            //where n.FeHasta < DateTime.Now && n.Activo


            var noticias = from n in entities.Noticias
                           where n.Activo && n.FeDesde <= DateTime.Now && DateTime.Now <= n.FeHasta
                           select n;
            var noticiasArray = noticias.ToArray();

            int pageSize = 10;
            int pageNumber = (page ?? 1);

            return View(noticiasArray.ToPagedList(pageNumber, pageSize));
        }
    }

这是我的观点

@model PagedList.IPagedList<IntranetCorporativa.Model.Noticias>
@using PagedList;
@using PagedList.Mvc;
@{
    var format = "dddd, MMMM dd, yyyy";
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    string principalTitulo = Model[0].Titulo;
    string principalContenido = Model[0].Contenido;
    DateTime principalFechaDesde = Convert.ToDateTime(Model[0].FeDesde);
    DateTime principalFechaHasta = Convert.ToDateTime(Model[0].FeHasta);
}

<script type="text/javascript">
    function changeDisplay(e) {

        var principalTitulo = $(e).text();
        var principalContenido = $(e).siblings(".vNoticiaContenido:first").html();
        var principalFecha = $(e).siblings(".vNoticiaFecha:first").val();

        $("#currentprincipalTitulo").html(principalTitulo);
        $("#currentprincipalContenido").html(principalContenido);
        $("#currentprincipalFecha").html(principalFecha);
    }
</script>

<style>
    .uppercase {
        text-transform: uppercase;
    }

    .limit {
        text-overflow: ellipsis;
        word-wrap: break-word;
        overflow: hidden;
        max-height: 3em;
        line-height: 1.7em;
    }
</style>
<!-- CONTENIDO -->
<div class="col-md-12 main">

    <div class="header sec-title-hd">
        <div class="bg-calendar"></div>
        <div class="col-md-7">
            <h5 class="pull-left">NOTICIAS</h5>
            <div>
                <a href="dashboard.html" class="btn sky-blue n-radius-b"> <img src="slider/img/arrow-left.png"> VOLVER</a>
            </div>
        </div>
    </div>

    <div class="content-inter">
        <div class="container-fluid sec-title-hd-sub">
            <div class="row">
                <div class="col-md-7">
                    <div>
                        <figure class="img_N">
                            <img id="currentprincipalImagen" src="#" class="img-responsive" alt="Here Principal IMG" />
                            <figcaption>
                                <p id="currentprincipalImagenTitulo">Here Img Description</p>
                            </figcaption>
                        </figure>
                    </div>
                    <div class="textnota">
                        <br>
                        <h5 id="currentprincipalTitulo" class="titulo_N uppercase">@principalTitulo</h5>
                        <p class="time">FeDesde: @principalFechaDesde.ToString(format)</p>
                        <p class="time">FeHasta: @principalFechaHasta.ToString(format)</p>
                        <p class="time">Hoy: @DateTime.Now.ToString(format)</p>
                        <div class="noti_P">
                            <p id="currentprincipalContenido">@principalContenido</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-5">
                    <!-- Lado Derecho -->
                    @foreach (IntranetCorporativa.Model.Noticias n in Model)
                    {
                        <blockquote class="blockquote-nopadding bg-calendar-border-left">
                            <p class="time_f principalTitulo">@n.FeDesde.ToString(format)</p>
                            <a href="#" onclick="changeDisplay(this)" class="titulo_N">@n.Titulo</a>
                            <p class="text-justify limit vNoticiaContenido">@n.Contenido</p>
                        </blockquote>
                    }
                    Págnia @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) de @Model.PageCount
                    @Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
                    <div>

                    </div>

                </div>
            </div>
        </div>
    </div>

</div>

感谢一切.

推荐答案

您将需要这样的视图模型:

you will need a view model like this:

internal class NewsImagesViewModel
{
    public string Title{ get; set; }

    public IEnumerable<Image> Images { get; set; }

    //... some other properties
}

在控制器中:

IList<NewsImagesViewModel> newsImagesList;

using (DbContext dbContext = new DbContext())
{
   newsImagesList = dbContext.News
       .Select(n => new NewsImagesViewModel
       {
           Title = n.Title,
           Images = n.Images,
           // ... some other properties you may need
       }
       .ToList();                                        
 }
 return View(newsImagesList);

在视图中

@model IEnumerable<Your.Namespace.NewsImagesViewModel>
@foreach(var item in Model)
{
 //....
}

这篇关于如何在MVC项目中使用Entity Framework 6和Linq合并2个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆