I have this model as you can see :
public class UserNotes : BaseEntity
{
public IList<SymbolNotes> SymbolsNotes { get; set; }
public UserNotes(string userId): base($"UserNotes/{userId}")
{
}
}
public class SymbolNotes
{
public string Isin { get; set; }
public List<NoteItem> Notes { get; set; }
}
Now in my code I declare a variable named userNotes
,
But when I put the point the SymbolsNotes
doesn't in the list why ?
CodePudding user response:
You have a variable userNotes
that contains a IList
of the class UserNotes
. To simplify, you can imagine that you have a collection of box on which you wrote an integer (the index of the box in the collection), and the content of each box you have is an instance of the UserNotes
class.
When you write userNotes.
, you target the collection of box. So to access the content of the class UserNotes
, you will have to target a specific box to look the content inside to access the instance. Then only you will be able to access the property SymbolsNotes
.
Another way of visualizing it is like a paper dictionary, it has a lot of translations but you cannot just say to the editor "change the description", you will have to say "change the description of the first word in the dictionary", or "change the description of the word 'banana' ".
In C#, you have multiple possibilities to do it :
- Selecting by the index
- Selecting the first item with the
FirstOrDefault
Linq method - Selecting one item that respects a condition with the
Single
Linq method
Of course many other way exists but I juste wrote 3 of the most commons ways to do it.
And you could it like this :
Use the index
userNotes[0].SymbolsNotes.Add(newSymbolNotes);
Use the First
Linq method
userNotes.First().SymbolsNotes.Add(newSymbolNotes);
use the Single
Linq method
userNotes.Single(/* Condition with lambda expression */).SymbolsNotes.Add(newSymbolNotes);