Created
April 22, 2017 17:00
-
-
Save caviles/94b688d0e8c81d1ed99cf94ffe13e341 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Student collection | |
IList<Student> studentList = new List<Student>>() { | |
new Student() { StudentID = 1, StudentName = "John", Age = 13} , | |
new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } , | |
new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } , | |
new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} , | |
new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 } | |
}; | |
// LINQ Query Syntax to find out teenager students | |
var teenAgerStudent = from s in studentList | |
where s.Age > 12 && s.Age < 20 | |
select s; // no library need built into the lang | |
//method based linq | |
// string collection | |
IList<string> stringList = new List<string>() { | |
"C# Tutorials", | |
"VB.NET Tutorials", | |
"Learn C++", | |
"MVC Tutorials" , | |
"Java" | |
}; | |
// LINQ Query Syntax | |
var result = stringList.Where(s => s.Contains("Tutorials")); //needs library system.net | |
all linq methods where implemented as extesion in the linq namspece in a class called enumerable, these can work on any | |
use firstordefault so you don't a run time error when oan object returned is null | |
foundCust = custList.FirstOrDefault(c => { | |
Debug.WriteLine(c.name) | |
return c.id == custid | |
});//lamda expression | |
//skip 1 and select the second | |
c.Where(b => b ==2).Skip(1).First() | |
linq provides transversal, filter and expression | |
namespace ExtensionMethods | |
{ | |
public static class MyExtensions | |
{ | |
public static int WordCount(this String str) | |
{ | |
return str.Split(new char[] { ' ', '.', '?' }, | |
StringSplitOptions.RemoveEmptyEntries).Length; | |
} | |
} | |
} | |
using ExtensionMethods | |
string s = "Hello Extension Methods"; | |
int i = s.WordCount(); | |
OrderBy The operator sort values in an ascending order orderby Order By | |
OrderByDescending The operator sort values in a descending order orderby ... descending Order By ... Descending | |
ThenBy Executes a secondary sorting in an ascending order orderby …, … Order By …, … | |
ThenByDescending Executes a secondary sorting in a descending order orderby …, … descending Order By …, … Descending | |
Reverse Performs a reversal of the order of the elements in a collection | |
two great testing tools | |
var ran = new Random(); | |
3: // does this generate 100 random numers? | |
4: // Or repeat first random number 100 times? | |
var repeatedRandom = Enumerable.Repeat(ran.Next(), 100); | |
var numbers = Enumerable.Range(1, 10).ToList(); | |
linq set operators | |
intersect - will find the items that are == in two list | |
except - wil give you the items not in the second list | |
concat - will merge sets | |
distinct - will give just that | |
union - will merge the unqiue items | |
join and create a new list and project new result | |
var results = workOrders.Join(plans, | |
wo => wo.WorkOrderNumber, | |
p => p.WorkOrderNumber, | |
(order,plan) => new {order.WorkOrderNumber, order.WorkDescription, plan.ScheduledDate} | |
); // this will return a enumerable with three properties work WorkOrderNumber WorkDescription ScheduledDate | |
//when we want to work with a collection within another collection we can use selectmany to flatten the object | |
// Select gets a list of lists of phone numbers | |
IEnumerable<IEnumerable<PhoneNumber>> phoneLists = people.Select(p => p.PhoneNumbers); // non flattened so you have an enumarble inside of another | |
// SelectMany flattens it to just a list of phone numbers. // now only one enumberable | |
//math with lamda | |
max - persons.Max(p => p.age); | |
min - persons.Min(p => p.age); | |
average - persons.Average(p => p.age); | |
any - persons.Any(p => p.age); // lazy check will only peek first rather than look at all items | |
count - persons.Count(p => p.age); // not lazy will count all | |
//group by and aggreagate | |
List<ResultLine> result = Lines | |
.GroupBy(l => l.ProductCode) | |
.Select(cl => new ResultLine | |
{ | |
ProductName = cl.First().Name, | |
Quantity = cl.Count().ToString(), | |
Price = cl.Sum(c => c.Price).ToString(), | |
}).ToList(); // when you group you only get one so you aggreate to get a true group by | |
IEnumerable<PhoneNumber> phoneNumbers = people.SelectMany(p => p.PhoneNumbers); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment