0

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.

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:

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.

But it is in my CarList model, I have it defined like so:

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!

SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185
  • 1
    Do you receive an error? If so, what is that error? Or is this an issue with Intellisense? – Nolan Bradshaw Oct 07 '20 at 15:51
  • Give us the code of all the classes. `CountryList`, `CarList` should be sufficient – A_kat Oct 07 '20 at 16:32
  • @NolanBradshaw I get a `CarList doesn't contain a definition for CarEngines` – SkyeBoniwell Oct 07 '20 at 16:57
  • @NolanBradshaw the full error in Visual Studio is: `Error CS1061 'ICollection' does not contain a definition for 'CarEngines' and no accessible extension method 'CarEngines' accepting a first argument of type 'ICollection' could be found ` – SkyeBoniwell Oct 07 '20 at 16:59
  • @A_kat thanks! I added those – SkyeBoniwell Oct 07 '20 at 17:08
  • Have you made any changes to the database without doing it through entity? This error often happens when the database has changed and EF is not aware of the change, therefore the mapping is incorrect. – Nolan Bradshaw Oct 07 '20 at 17:12

1 Answers1

2

Ok so the fact that there is something called CarList but is not a List is super confusing but moving on....

The issue is that CarList is a List. So use something like 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.

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

A_kat
  • 1,459
  • 10
  • 17
  • Thanks I will try it out, some of the code you see is what .NetCore EF autogenerated when I created the project and ran the .net core database scaffold command. Like the HashSet thing. :) – SkyeBoniwell Oct 07 '20 at 18:07
  • Would I use `factoryCars.CarList.Select( x=>x.CarEngines)` in place of the `ThenInclude` line or would I put it below it? Thanks! – SkyeBoniwell Oct 07 '20 at 18:12
  • Furthermore you can use the `Table` attribute to so you can safely rename classes – A_kat Oct 07 '20 at 18:13
  • thanks! So I added that...will it order automatically or do I need to create another object to order by? – SkyeBoniwell Oct 07 '20 at 18:31