以多对多关系获取所有实体的数据

时间:2017-10-25 07:28:27

标签: mysql asp.net-mvc entity-framework many-to-many

picture of DB我的数据库有3个表客户,电影和客户电影现在是电影和客户之间的桥梁表,我想用他们租来的电影来显示一个角色表。 我写了一个函数来完成特定id的工作(在代码8和9中)。我的问题是如何向所有实体显示数据?

   public ActionResult GetData()
    {
        MyDatabaseEntities2 db = new MyDatabaseEntities2();
        var q = (from c in db.Customers
                 from Films in db.Films
                     where Films.FilmId == 8
                     where c.CustomerId == 9
                 select new
                 {
                     c.CustomerName,
                     c.Phone,
                     c.FilmId,
                     c.CustomerId,
                      FilmName = Films.FilmName

                 }).ToList();
        return Json(new { data = q }, JsonRequestBehavior.AllowGet);
    }

添加我的课程

namespace MoviePro.Models
{
using System;
using System.Collections.Generic;

public partial class Films
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", 
 "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Films()
    {
        this.Customers = new HashSet<Customers>();
    }

    public int FilmId { get; set; }
    public string FilmName { get; set; }
    public string Length { get; set; }
    public string Genre { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", 
    "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Customers> Customers { get; set; }
    }
    }
    public partial class Customers
    {
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", 
    "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Customers()
    {
        this.Films = new HashSet<Films>();
    }

    public int CustomerId { get; set; }
    public string CustomerName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public Nullable<int> FilmId { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", 
    "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Films> Films { get; set; }
    }
    }

非常感谢你,

****新的代码****

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

namespace MoviePro.Controllers
{
public class CustomersController : Controller
{
    // GET: Customers
    public ActionResult Index()
    {
        return View();
    }
    public ActionResult GetData()
    {
        MyDatabaseEntities2 db = new MyDatabaseEntities2();
        var q = (from c in db.Customers
                 from Films in c.Films
                 where Films.FilmId == Films.FilmId
                 where c.CustomerId == c.CustomerId
                 select new
                 {
                     c.CustomerName,
                     c.Phone,
                     c.FilmId,
                     c.CustomerId,
                     FilmName = Films.FilmName

                 }).ToList();
        return Json(new { data = q }, JsonRequestBehavior.AllowGet);
    }

    public ActionResult loaddata()
    {
        MyDatabaseEntities2 dc = new MyDatabaseEntities2();

        var customers = dc.Customers.Select(c => new
        {
            c.CustomerName,
            c.Phone,
            c.FilmId,
            c.CustomerId,


        });

        return Json(new { data = customers }, JsonRequestBehavior.AllowGet);

    }

    public ActionResult AddOrEdit(int id = 0)
    {
        if (id == 0)
            return View(new Customers());

        else
        {
            using (MyDatabaseEntities2 db = new MyDatabaseEntities2())
            {
                return View(db.Customers.Where(x => x.CustomerId== 
   id).FirstOrDefault<Customers>());

            }
        }
    }

    [HttpPost]
    public ActionResult AddOrEdit(Customers customer)
    {
        using (MyDatabaseEntities2 db = new MyDatabaseEntities2())
        {
            if (customer.CustomerId == 0)
            { 
                db.Customers.Add(customer);
                db.SaveChanges();
                return Json(new { success = true, message = "Saved 
      Successfully" }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                db.Entry(customer).State = EntityState.Modified;
                db.SaveChanges();
                return Json(new { success = true, message = "Updated 
        Successfully" }, JsonRequestBehavior.AllowGet);
            }
        }


    }

    [HttpPost]
    public ActionResult Delete(int id)
    {
        using (MyDatabaseEntities2 db = new MyDatabaseEntities2())
        {
            Customers emp = db.Customers.Where(x => x.CustomerId == 
      id).FirstOrDefault<Customers>();

            db.Customers.Remove(emp);
            db.SaveChanges();
            return Json(new { success = true, message = "Deleted 
       Successfully" }, JsonRequestBehavior.AllowGet);
        }
        }
       }
       }

1 个答案:

答案 0 :(得分:1)

用于查询多对多关系的基本LINQ查询形状是:

from c in db.Customers
from f in c.Films // NOT db.Films
select new
{
    Customer = c.Customername,
    Film = f.FilmName,
    ...
}

这是等同于SelectMany的查询语法。结果是一个平坦的数据列表,因此这通常被称为“扁平化”。

显示嵌套列表通常更有用:

from c in db.Customers
select new
{
    c.Customername,
    c. ...,
    Films = from f in c.Films
            select new
            {
                f.FilmName,
                f. ...,
            }
}