-
Notifications
You must be signed in to change notification settings - Fork 526
LINQ
Language Integrated Queries ("LINQ") is a set of language features and extension methods in System.Linq
designed to query over collections and operate on their elements, or more correctly, any type which implements IEnumerable<T>
(a sequence).
sequence - Any type which implements
IEnumerable<T>
, an object which can be iterated over withforeach
element - Any object in a sequence
LINQ can often replace a loop, reducing program complexity in the process.
To use LINQ, System.Linq
must be referenced by your program. Here's a short example to shows usage of one of LINQ's fundamental methods, Where()
. Not to worry if you don't quite understand what you see here yet.
using System.Collections.Generic;
using System.Linq;
namespace LinqExample
{
class Program
{
static void Main(string[] args)
{
int[] values = new[] { 1, 2, 4, 9, 16, 25, 36 };
// defines a sequence using LINQ
IEnumerable<int> numbersOver24 = values
.Where(x => x > 24);
// forces the sequence to execute
// contains { 25, 36 }
int[] numbersOver24Array = numbersOver24.ToArray();
}
}
}
This example shows one more import concept in LINQ, deferred executed. Sequences created by LINQ are generally not executed until they must. LINQ methods which force execution will be noted in the examples.
TODO
Filters a sequence based on the predicate passed into Where()
.
predicate - A function that accepts an element as the input argument, and returns true or false
Elements are in the resulting output sequence if the predicate returns true
.
You have a requirement to implement a program that returns all even numbered integers in a sequence.
IEnumerable<int> GetEvenNumbers(IEnumerable<int> sequence)
{
var list = new List<int>();
foreach (var number in sequence)
{
if (number % 2 == 0)
{
list.Add(number);
}
}
return list;
}
IEnumerable<int> GetEvenNumbers(IEnumerable<int> sequence)
{
return sequence.Where(x => x % 2 == 0);
}
Transforms each element of the sequence to a new type, using the selector
function passed in.
You need to implement converting a day-of-week code (M - Monday, T - Tuesday, etc.) to a enum, DayOfWeek
.
enum DayOfWeek
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday,
Unknown
}
IEnumerable<DayOfWeek> ConvertToDaysOfWeek(string codes)
{
var daysOfWeek = new List<DayOfWeek>();
foreach (var dayCode in codes)
{
var valid = DaysOfWeekMap.TryGetValue(dayCode, out var dayOfWeek);
daysOfWeek.Add(valid ? dayOfWeek : DayOfWeek.Unknown);
}
return daysOfWeek;
}
IEnumerable<DayOfWeek> ConvertToDaysOfWeek(string codes)
{
DayOfWeek Convert(char code)
{
var valid = DaysOfWeekMap.TryGetValue(code, out var dayOfWeek);
return valid ? dayOfWeek : DayOfWeek.Unknown;
}
return codes.Select(Convert);
}
TODO
Chapter 1: Fundamentals
Chapter 2: Object Oriented Programming (to be started 2/19/2020)
Chapter 3: Intermediate C# (Q2)
Chapter 4: Testing (Q2)
Chapter 5: SOLID (Q2)
Chapter 6: Desktop UI (Q2)
Chapter 7: Design Patterns (Q3)
Chapter 8: EF and enterprise patterns (Q3)
Chapter 9: Async programming (Q3)
Chapter 10: Web Api (Q3)
Chapter 11: Security (Q4)