Ruben
Ruben

Reputation: 155

Assign single object to list of object

I have a model with a simple Guid:

public class GuidIdTableType
    {
        public Guid Id { get; set; }
    }

Then I have a model where I create a list of model used before:

public class SelectModel
        {
          ....
    public List<GuidIdTableType> MyGuidTableTypeList { get; set; } = new List<GuidIdTableType>();

        }

Now I get a list of objects in my code like:

  var currentModel = _myRepository.Get(model);

And I want to fill my list of guids with an object inside that list, So I try:

 var model = new SelectModel();
                foreach(var i in currentModel.Result)
                {
                    var rModel = new SelectModel();

                    rModel.MyGuidTableTypeList = i.Assignee;

                    model.MyGuidTableTypeList.Add(rModel);
                }

But it throws an error

Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type 'System.Guid' to 'System.Collections.Generic.List'

What am I doing wrong?. Regards

Upvotes: 1

Views: 469

Answers (1)

Rohit Garg
Rohit Garg

Reputation: 493

It looks like you are adding 'rModel' to GuidIdTableType property of 'model'. rModel and model are of same type i.e. SelectModel. It doesn't make sense. You may want to try something like below:

var model = new SelectModel();
                foreach(var i in currentModel.Result)
                { 

                    model.MyGuidTableTypeList.Add(new GuidIdTableType{id = i.Assignee}); // considering i.Assignee is Guid
                }

Upvotes: 4

Related Questions