Carlove
Carlove

Reputation: 449

C# "cannot implicitly convert type" same model properties

I'm trying to use a ViewModel in C# MVC .NET Visual Studio. I have this mode that's filled with data. It's built up like so:

public class Offer
{
    public List<ProjectleaderModel> Projectleaders { get; set; } = new List<ProjectleaderModel>();
    public List<OfferManagerModel> OfferManagers { get; set; } = new List<OfferManagerModel>();
    public List<ProductModel> Products { get; set; } = new List<ProductModel>();
    public List<PersonModel> People { get; set; } = new List<PersonModel>();
    public string Administration { get; set; }
    public string Status { get; set; }
    public DateTime Applicationdate { get; set; }
    public DateTime Submitdate { get; set; }
    public Boolean CustomerAgreementBoolean { get; set; }
    public DateTime CustomerAgreementBooleanDate { get; set; }



}

public class ProjectleaderModel
{
    public int Id;
    public string Name;

}

And in my viewmodel I'm trying to get data into a separate list I want to turn into a dropdown-menu:

    public List<Offer> Offermodel = new List<Offer>();
    public List<ProjectleaderModel> Projectleaders
    {
        get
        {
            return Offermodel.Select(x => x.Projectleaders).ToList();
        }
    }

The Projectleadermodel class is in the model, as you can see. Why is it that I'm getting the error:

Cannot implicitly convert type 'System.Collections.Generic.List<System.Collections.Generic.List<EnqueryApp.Model.ProjectleaderModel>>' to 'System.Collections.Generic.List<EnqueryApp.Model.ProjectleaderModel>'

Upvotes: 2

Views: 5704

Answers (4)

Joe T.
Joe T.

Reputation: 43

   public List<Offer> Offermodel = new List<Offer>();
    public List<List<ProjectleaderModel>> Projectleaders
    {
        get
        {
            return Offermodel.Select(x => x.Projectleaders).ToList();
        }
    }

What you're doing is trying to get a List of the Project Leaders, but Project Leaders is already a list object, so you're getting a list of a list.

Upvotes: 0

orhtej2
orhtej2

Reputation: 2165

Notice that Offer.Projectleaders is of type List<ProjectleaderModel>, hence your Select will yield IEnumerable<List<ProjectleaderModel>> instead of desired IEnumerable<ProjectleaderModel>. Linq has a way around that in form of SelectMany extension method.

Given that your Projectleaders can read

public List<ProjectleaderModel> Projectleaders =>
        Offermodel.SelectMany(x => x.Projectleaders).ToList();

Upvotes: 1

devdigital
devdigital

Reputation: 34369

If you want all project leaders from all offers, then use SelectMany

return Offermodel.SelectMany(x => x.Projectleaders).ToList();

Upvotes: 3

Sach
Sach

Reputation: 10393

The line Offermodel.Select(x => x.Projectleaders) will give you a collection of Offer class objects, which then you're making a list of.

But the return type of your Projectleaders property is List<ProjectleaderModel>

So they aren't a match.

You need to create a list of Projectleaders objects to return from that property.

Essentially:

return Offermodel.SelectMany(x => x.Projectleaders).ToList();

Upvotes: 4

Related Questions