[英]child relationship not available in parent object
I have the following method that returns a list of factory cars.我有以下返回工厂汽车列表的方法。
It works, but the ordering is wrong.它有效,但顺序错误。
CarEngines can have an orderId and I want to order by that. CarEngines 可以有一个 orderId,我想通过它来订购。
Looking at other answers on here, I see that you can't do an order by inside the query and you have to do it afterwards.看看这里的其他答案,我发现你不能在查询中进行排序,你必须在之后进行。
The problem is, I can't access CarEngines
as you can see below:问题是,我无法访问
CarEngines
,如下所示:
public async Task<ActionResult<CountryList>> GetCountryCarObject(Guid countryID)
{
var factoryCars = await _context.CountryList
.Include(n => n.CarList).ThenInclude(l => l.CarEngines)
.Include(n => n.CarList).ThenInclude(l => l.CarOptions)
.SingleOrDefaultAsync(c => c.CountryId == countryID);
factoryCars.CarList.CarEngines <== CarEngines doesn't show up in CarList object
return factoryCars;
}
It is telling me that CarList doesn't contain a definition for CarEngines.它告诉我 CarList 不包含 CarEngines 的定义。
But it is in my CarList model, I have it defined like so:但它在我的 CarList model 中,我是这样定义的:
public CarList()
{
CarEngines = new HashSet<CarEngines>();
}
public virtual ICollection<CarEngines> CarEngines { get; set; }
Here are the two models:这是两个模型:
public partial class CarList
{
public CarList()
{
CarEngines = new HashSet<CarEngines>();
CarOptions = new HashSet<CarOptions>();
}
public string CarId { get; set; }
public string CarMake { get; set; }
public string CarModel { get; set; }
public Guid? CarCountryId { get; set; }
public virtual ICollection<CarEngines> CarEngines { get; set; }
public ICollection<CarOptions> CarOptions { get; set; }
}
public partial class CountryList
{
public CountryList()
{
CarList = new HashSet<CarList>();
}
[Key]
public Guid CountryId { get; set; }
public string CountryName { get; set; }
public string CountryLocation { get; set; }
public string CountryDesc { get; set; }
public virtual ICollection<CarList> CarList { get; set; }
}
So I'm not sure it doesn't see it.所以我不确定它没有看到它。
Is there a way to get this to work?有没有办法让它工作?
Thanks!谢谢!
Ok so the fact that there is something called CarList
but is not a List is super confusing but moving on....好吧,事实上有一个叫做
CarList
但不是 List 的东西非常令人困惑但继续......
The issue is that CarList
is a List.问题是
CarList
是一个列表。 So use something like factoryCars.CarList.Select( x=>x.CarEngines)
.所以使用类似
factoryCars.CarList.Select( x=>x.CarEngines)
的东西。 Also rename that to var country
instead of var factoryCars
since you return a single country and not a list of cars.还要将其重命名为
var country
而不是var factoryCars
,因为您返回的是一个国家而不是汽车列表。
Also rename your variables and classes this confusion was probably caused by this.同时重命名您的变量和类,这种混淆可能是由此引起的。 For example instead of having
ICollection<CarList> CarList
you can rename it into ICollection<Car> Cars
so right now from the name you can easilly understand there are multiple cars (thus its a collection) which includes the object Car
例如,您可以将其重命名为
ICollection<Car> Cars
<CarList> CarList 而不是ICollection<CarList> CarList
,因此现在从名称中您可以轻松地理解有多辆汽车(因此它是一个集合),其中包括 object Car
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.