GetData来自多对多映射表

时间:2017-10-24 19:21:51

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

我是实体框架的新手, 我制作了3张桌子CustomersFilms is the brige table between Films and Customers 现在,我想用他们租来的电影来展示一个人物表。 我希望从Customers表中显示数据。但我无法想出正确的语法来从customerFilms获取数据。 有人可以帮忙吗? 这是customerController的代码,用于从customers表中获取数据:

       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);

    }

I tried this function, but I get the same user in the table

   public ActionResult GetData()
    {
        MyDatabaseEntities2 db = new MyDatabaseEntities2();
        var q = (from Customers in db.Customers
                 from Films in db.Films
                 select new {
                     FilmName = Films.FilmName,
                     CustomerName = Customers.CustomerName
                 }).ToList();
        return Json(new { data = q }, JsonRequestBehavior.AllowGet);
    }

0 个答案:

没有答案