简体   繁体   English

从模型中选择所有行,并将嵌套的json元素作为另一个模型的外键,并选择所有行

[英]Select all rows from a model and have a nested json element as the Foreign key to another model with all rows selected

I would like my JSON element returned by my controller to look like this 我希望控制器返回的JSON元素如下所示

{           
    "model": "Ford",
    "make": "GT350R",
    "dealerName": "Dallas Ford",
    "location": {
        "dealerName": "Dallas Ford",
        "zip": 05700,
    }
}

I cant seem to get the query right in my controller. 我似乎无法在我的控制器中正确获取查询。 I want to select all rows and every element from the Cars Model and where there is a foreign key to the location Table. 我想从“汽车模型”中选择所有行和每个元素,以及在位置表中有外键的位置。 I then want to embed all the rows that belong to that location element into a sub element. 然后,我想将属于该location元素的所有行嵌入到sub元素中。 I think i may need to tell my models about the foreign key but because i am using the same attribute name i think Entity Framework 6 does it for me. 我认为我可能需要告诉我的模型有关外键的信息,但是因为我使用的是相同的属性名,所以我认为Entity Framework 6可以为我做到这一点。

This is the code I am trying which will only select all the results from Cars it wont show the location as a nested element. 这是我正在尝试的代码,它将仅从Cars中选择所有结果,而不会将位置显示为嵌套元素。

var query = (from results in db.Cars
             join location in db.Locations on results.DealerName equals location.Dealername
             select results);
return Ok(query.ToList());

This is the json that it shows for each element 这是它为每个元素显示的json

{ 
    "model": "Ford",
    "make": "GT350R",
    "dealerName": "Dallas Ford",
}

Here are my models 这是我的模特

Car model 汽车模型

//......
[Key]
public string make { get; set; }
public string model { get; set; }
public Location Locations{ get; set; }//added this because someone in comments said to
public string dealerName { get; set; }
//........

Location model 位置模型

//......
[Key]
public string dealername{ get; set; }
public string Zip { get; set; }
//........

And heres what i think the code should look like but I am not doing it right. 我认为代码看起来应该是这样,但我做的不正确。

var query = (from results in db.Cars
             join location in db.Locations on results.DealerName equals location.Dealername
             select new {
                 results = results;
                 results.location = location;
             });
return Ok(query.ToList());

Try this 尝试这个

    var query = (from results in db.Cars
                 join location in db.Locations on results.dealerName equals location.dealerName 
                 select new {
                   model = results.model, make = results.make, dealerName = results.dealerName, location = location }     
             );
                return json(query.ToList());

Make your life simpler and use the Entity Framework syntax. 使您的生活更简单,并使用实体框架语法。 Even though you can use LINQ to SQL, it's just so much more verbose and problematic to work with. 即使可以使用LINQ to SQL,使用起来也很冗长和麻烦。 All you need is: 所有你需要的是:

var query = db.Cars.Where(m => m.Locations.Any()).Include(m => m.Locations);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM