# List Operation
###### tags: `C#`, `UWP`
### Adding Items
`Add` : add the item to the end of the list
```csharp
List<string> myList = new List<string>();
myList.Add("a");
myList.Add("b");
// ["a", "b"]
```
`Insert` : add the item at the specified index
```csharp
myList.Insert(0,"c");
// ["c", "a", "b"]
```
`AddRange`: concat two lists
```csharp
string[] newList = {"t", "e", "s"};
myList.AddRange(newList);
// ["c", "a", "b", "t", "e", "s"]
```
### Selecting Items
`Where` : return items that matches the condition
```csharp
// Find all the people older than 30
var query1 = myList.Where(person => person.Age > 30);
```
`Select` : return specified property
```csharp
// Find each person's name
var query2 = myList.Select(person => person.Name);
```
[reference](https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.select?view=netframework-4.8)
### Finding Items
`Find` / `FirstOrDefaut`: The first **element** that matches the conditions; otherwise, the default value for type T.
```csharp
myList.Find(x => x.id == 3));
myList.FirstOrDefault(x => x.id == 3)); // LINQ
```
[Difference between Find and FirstOrDefault](https://stackoverflow.com/questions/14032709/performance-of-find-vs-firstordefault)
`FindIndex` / `IndexOf`: The zero-based **index** of the first occurrence of an element; otherwise, -1.
```csharp
myList.IndexOf(myItem);
myList.FindIndex(i => i.id == 3);
```
`Distinct`: select distinct values.
```csharp
myList.Select(i => i.id).Distinct();
```
### Aggregate
遞迴操作
```csharp
var numbers = new List<int> { 6, 2, 8, 3 };
int sum = numbers.Aggregate(func: (result, item) => result + item);
// sum: (((6+2)+8)+3) = 19
```
### Condition
`All`: Determines whether all elements of a sequence satisfy a condition.
```csharp
bool allSmallerthan5 = numbers.All(i => i < 5);
// false
```
`Any` : Determines whether a sequence contains any elements.
```csharp
bool hasItem = numbers.Any();
// true
```
### 平行運算
`AsParallel()` : performance will be better, but items not in order
```csharp
resultList = testData.AsParallel().Where(a => a.Contains("a"))
```
## 畫面
### Situation
we have a list with another list inside, we want to change itmes in the inner list and update our ui.
```csharp
List<OuterClass> OuterList
public class OuterClass
{
public List<InnerClass> innerList;
public string Key ;
}
public class InnerClass
{
public string name;
}
```
| Outer list | Inner list | OuterClass |InnerClass | Result |
| -------- | -------- | -------- |--|--|
| List | OC/List | -/Observable |-/Observable| :no_entry_sign: |
|ObservableCollection|OC/List|-|-| :no_entry_sign: can Add/remove items in OuterList|
|ObservableCollection|List|Observable| -/Observable| :no_entry_sign: can Add/remove/assign items in OuterList|
|ObservableCollection|OC|Observable|-| :no_entry_sign: can Add/remove/assign items in OuterList, Add/remove items in innerList|
|ObservableCollection|OC|Observable|Observable| :white_check_mark: can Add/remove/assign items in OuterList, Add/remove/assign items in innerList|