如何在DB中获取属性对象的属性

时间:2021-07-08 19:40:38

I'm looking for sometime now and I can't find any solution for my problem, even in this post:

我现在正在寻找,我找不到任何解决方案,即使在这篇文章中:

asp.net mvc 3 - How do you load children AND grand-children of an entity?

asp.net mvc 3 - 如何加载实体的子项和子项?

Basically I have 3 classes:

基本上我有3个班:

Offer.cs, Application.cs, and Contractor.cs

Offer.cs,Application.cs和Contractor.cs

1 Offer contains Collection of Aplications

1项优惠包含应用程序集

1 Application contains 1 Contractor.

1申请包含1个承包商。


Basically I want to grab in the View the Contractor Object which is coming as null. Thanks for any help!!!!

基本上我想抓住查看承包商对象,它将变为空。谢谢你的帮助!!!!

Here goes the code:

这里是代码:

View

@foreach (var item in Model)
{
foreach (var candidate in item.Candidates)
    {
       Here is the problem #### canditate.Contractor == null ###

Controller

public ActionResult Index () {
 return View (db.Offers.Include(c => c.Location).Include(c => c.Candidates).ToList ());
}

Model

public class Offer {

public Location Location { get; set }
public ICollection<Application> Candidates { get; set; }

}

public class Application {
    public Contractor Contractor { get; set}
}

2 个解决方案

#1


0  

You can try

你可以试试

db.Offers.Include("Location.Candidates").ToList()

This should load all the Locations and all the related Candidates for those Locations (although I do not know of a method to do this using lambdas).

这应该为这些位置加载所有位置和所有相关的候选者(虽然我不知道使用lambdas执行此操作的方法)。

#2


0  

You can use...

您可以使用...

return View(db.Offers
    .Include(o => o.Location)
    .Include(o => o.Candidates.Select(c => c.Contractor))
    .ToList());

... in your Index action in order to load the Contractor grand children together with the Candidates children.

...在您的索引操作中,以便将承包商的大孩子与候选人孩子一起加载。

#1


0  

You can try

你可以试试

db.Offers.Include("Location.Candidates").ToList()

This should load all the Locations and all the related Candidates for those Locations (although I do not know of a method to do this using lambdas).

这应该为这些位置加载所有位置和所有相关的候选者(虽然我不知道使用lambdas执行此操作的方法)。

#2


0  

You can use...

您可以使用...

return View(db.Offers
    .Include(o => o.Location)
    .Include(o => o.Candidates.Select(c => c.Contractor))
    .ToList());

... in your Index action in order to load the Contractor grand children together with the Candidates children.

...在您的索引操作中,以便将承包商的大孩子与候选人孩子一起加载。