# IEnumerable

***

1. **Iterating Over a List:** Iterate over a list of elements using `foreach` loop.

   ```c#
   List<string> names = new List<string> { "John", "Mary", "Bob" };
   foreach (string name in names)
   {
       Console.WriteLine(name);
   }
   ```
2. **Creating a Query:** Use LINQ to create a query that retrieves all elements from a collection.

   ```c#
   IEnumerable<string> query = from name in names
                               where name.Length > 3
                               select name;
   ```
3. **Filtering a Collection:** Filter a collection of elements based on a predicate using `Where` method.

   ```c#
   IEnumerable<string> filteredNames = names.Where(n => n.StartsWith("J"));
   ```
4. **Projecting a Collection:** Project a collection of elements into a new collection of values using `Select` method.

   ```c#
   IEnumerable<int> lengths = names.Select(n => n.Length);
   ```
5. **Joining Two Collections:** Join two collections of elements based on a key using `Join` method.

   ```c#
   List<Person> people = new List<Person> { new Person { Name = "John", Age = 30 }, new Person { Name = "Mary", Age = 25 } };
   List<Address> addresses = new List<Address> { new Address { PersonName = "John", City = "New York" }, new Address { PersonName = "Mary", City = "London" } };
   var joinedData = people.Join(addresses, p => p.Name, a => a.PersonName, (p, a) => new { Name = p.Name, Age = p.Age, City = a.City });
   ```
6. **Grouping a Collection:** Group a collection of elements by a key using `GroupBy` method.

   ```c#
   var groupedNames = names.GroupBy(n => n.Substring(0, 1));
   ```
7. **Aggregating a Collection:** Aggregate a collection of elements into a single value using `Aggregate` method.

   ```c#
   int totalLength = names.Aggregate(0, (acc, n) => acc + n.Length);
   ```
8. **Concatenating Collections:** Concatenate two collections of elements using `Concat` method.

   ```c#
   IEnumerable<int> numbers1 = new List<int> { 1, 2, 3 };
   IEnumerable<int> numbers2 = new List<int> { 4, 5, 6 };
   var concatenatedNumbers = numbers1.Concat(numbers2);
   ```
9. **Intersecting Collections:** Intersect two collections of elements to get common elements using `Intersect` method.

   ```c#
   var intersectedNumbers = numbers1.Intersect(numbers2);
   ```
10. **Unioning Collections:** Union two collections of elements to get all distinct elements using `Union` method.

```c#
var unionedNumbers = numbers1.Union(numbers2);
```

11. **Excepting Collections:** Except two collections of elements to get elements that are not in both collections using `Except` method.

```c#
var exceptedNumbers = numbers1.Except(numbers2);
```

12. **Distincting Collections:** Remove duplicate elements from a collection using `Distinct` method.

```c#
var distinctNames = names.Distinct();
```

13. **Reversing a Collection:** Reverse the order of elements in a collection using `Reverse` method.

```c#
var reversedNames = names.Reverse();
```

14. **Sorting a Collection:** Sort a collection of elements using `OrderBy` or `OrderByDescending` methods.

```c#
var sortedNames = names.OrderBy(n => n);
```

15. **Shuffling a Collection:** Shuffle the order of elements in a collection using `OrderBy` and `Guid` method.

```c#
var shuffledNames = names.OrderBy(n => Guid.NewGuid());
```

16. **Paging a Collection:** Page a collection of elements into smaller chunks using `Skip` and `Take` methods.

```c#
var pagedNames = names.Skip(10).Take(10);
```

17. **Converting to an Array:** Convert a collection of elements to an array using `ToArray` method.

```c#
string[] namesArray = names.ToArray();
```

18. **Converting to a List:** Convert a collection of elements to a list using `ToList` method.

```c#
List<string> namesList = names.ToList();
```

19. **Converting to a Dictionary:** Convert a collection of elements to a dictionary using `ToDictionary` method.

```c#
Dictionary<string, int> nameLengths = names.ToDictionary(n => n, n => n.Length);
```

20. **Converting to a HashSet:** Convert a collection of elements to a hash set using `ToHashSet` method.

```c#
HashSet<string> uniqueNames = names.ToHashSet();
```

21. **Converting to a Stack:** Convert a collection of elements to a stack using `ToStack` method.

```c#
Stack<string> namesStack = names.ToStack();
```

22. **Converting to a Queue:** Convert a collection of elements to a queue using `ToQueue` method.

```c#
Queue<string> namesQueue = names.ToQueue();
```

23. **Serializing to JSON:** Serialize a collection of elements to JSON using `JsonConvert.SerializeObject` method.

```c#
string jsonNames = JsonConvert.SerializeObject(names);
```

24. **Deserializing from JSON:** Deserialize a JSON string into a collection of elements using `JsonConvert.DeserializeObject` method.

```c#
List<string> deserializedNames = JsonConvert.DeserializeObject<List<string>>(jsonNames);
```

25. **Caching a Collection:** Cache a collection of elements in memory using `MemoryCache` class.

```c#
var cache = new MemoryCache(new MemoryCacheOptions());
cache.Set("names", names, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(10) });
```

26. **Iterating Over a Lazy Collection:** Iterate over a lazy collection of elements without loading all elements into memory.

```c#
IEnumerable<int> lazyNumbers = Enumerable.Range(1, 1000000);
foreach (int number in lazyNumbers)
{
    Console.WriteLine(number);
}
```

27. **Using a Range:** Generate a range of numbers using `Enumerable.Range` method.

```c#
IEnumerable<int> numbers = Enumerable.Range(1, 10);
```

28. **Using a Repeat:** Repeat an element a specified number of times using `Enumerable.Repeat` method.

```c#
IEnumerable<int> numbers = Enumerable.Repeat(1, 10);

```
