1

I have this class:

ActiveCampaign
{
     public Campaign campaign;
     public IEnumerable<CampaignView> Views;
}

ac is type of ActiveCampaign[]

The following code executes without any errors but nothing is added to its views. What amd I missing here?

ac.FirstOrDefault(a => a.Campaign.Id == c.Id).Views.ToList().Add(cv);

2 Answers 2

2

When you call ToList() you're creating a separate list; you're not adding to ac. You need to assign the return value:

List<ActiveCampaign> newList = ac.FirstOrDefault(a => a.Campaign.Id == c.Id).Views.ToList();

and then you can use it...

newList.Add(cv);
Sign up to request clarification or add additional context in comments.

1 Comment

I get an error Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<ActiveCampaign>'
1

You're creating a new list that is a copy of the Views property, then adding to it, then throwing that list away.

If you want to permanently affect Views then make it a List or IList type and add to it directly.

If you want to see the views plus that new item then get the list into a variable before the add, or use Concat (or even better Append though it's currently only available in dotnet core) to create an enumeration of the views items along with the addition.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.