Csharp/C Sharp by API/System.Linq/Enumerable
Содержание
- 1 Enumerable.Aggregate
- 2 Enumerable.Average()
- 3 Enumerable.Cast
- 4 Enumerable.Count()
- 5 Enumerable.DefaultIfEmpty
- 6 Enumerable.Distinct()
- 7 Enumerable.ElementAt
- 8 Enumerable.ElementAtOrDefault
- 9 Enumerable.ElementsAfterSelf
- 10 Enumerable.Empty
- 11 Enumerable.First
- 12 Enumerable.OrderBy
- 13 Enumerable.Range
- 14 Enumerable.Repeat
- 15 Enumerable.Where
Enumerable.Aggregate
<source lang="csharp">
using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; public class MainClass {
public static void Main() { int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var query = numbers.Aggregate((a, b) => a * b); }
}
</source>
Enumerable.Average()
<source lang="csharp"> using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass {
public static void Main() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; double averageNum = numbers.Average(); Console.WriteLine("The average number is {0}.", averageNum); }
}
</source>
Enumerable.Cast
<source lang="csharp"> using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; public class MainClass{
public static void Main(){ object[] doubles = {1.0, 2.0, 3.0}; IEnumerable<double> d = doubles.Cast<double>(); Console.Write(d); }
}
</source>
Enumerable.Count()
<source lang="csharp"> using System; using System.Collections.Generic; using System.Linq; static class TestString {
static void Main() { var count = "abc 8" .Where(c => !Char.IsLetter(c)) .Count(); Console.WriteLine(count); }
}
</source>
Enumerable.DefaultIfEmpty
<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass {
public static void Main() { string[] presidents = {"G", "H", "a", "H", "over", "Jack"}; string name = presidents.Where(n => n.Equals("H")).DefaultIfEmpty("Missing").First(); Console.WriteLine(name); }
}
</source>
Enumerable.Distinct()
<source lang="csharp"> using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; public class MainClass{
public static void Main(){ int[] numbers = {1, 1, 2, 3, 3}; Console.Write(numbers.Distinct()); }
}
</source>
Enumerable.ElementAt
<source lang="csharp"> using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; public class MainClass{
public static void Main(){ int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}; var query = numbers.ElementAt(4); Console.Write(query); }
}
</source>
Enumerable.ElementAtOrDefault
<source lang="csharp"> using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; public class MainClass{
public static void Main(){ int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}; var query = numbers.ElementAtOrDefault(9); Console.Write(query); }
}
</source>
Enumerable.ElementsAfterSelf
<source lang="csharp">
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {
public static void Main() { XElement firstParticipant; XDocument xDocument = new XDocument( new XDeclaration("1.0", "UTF-8", "yes"), new XDocumentType("Books", null, "Books.dtd", null), new XProcessingInstruction("Book", "out-of-print"), new XElement("Books", new XComment("Begin Of List"), firstParticipant = new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "J"), new XElement("LastName", "R")), new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "E"), new XElement("LastName", "B")), new XComment("End Of List"))); foreach (XNode node in firstParticipant.ElementsAfterSelf()) { Console.WriteLine(node); } }
}
</source>
Enumerable.Empty
<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass {
public static void Main() { IEnumerable<string> strings = Enumerable.Empty<string>(); foreach (string s in strings) Console.WriteLine(s); Console.WriteLine(strings.Count()); }
}
</source>
Enumerable.First
<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass {
public static void Main() { string[] presidents = {"G", "H", "a", "H", "over", "Jack"}; string jones = presidents.Where(n => n.Equals("H")).First(); if (jones != null) Console.WriteLine("H was found"); else Console.WriteLine("H was not found"); }
}
</source>
Enumerable.OrderBy
<source lang="csharp"> using System; using System.Linq; static class TestArray {
static void Main() { Object[] array = { "String", 12, true, "a" }; var types = array .Select(item => item.GetType().Name) .OrderBy(type => type); }
}
</source>
Enumerable.Range
<source lang="csharp"> using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass {
public static void Main() { var numbers = from n in Enumerable.Range(100, 50) select new { Number = n, OddEven = n % 2 == 1 ? "odd" : "even" }; foreach (var n in numbers) { Console.WriteLine("The number {0} is {1}.", n.Number, n.OddEven); } }
}
</source>
Enumerable.Repeat
<source lang="csharp"> using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass {
public static void Main() { var numbers = Enumerable.Repeat(7, 10); foreach (var n in numbers) { Console.WriteLine(n); } }
}
</source>
Enumerable.Where
<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass {
public static void Main() { string[] strings = { "one", "two", null, "three" }; Console.WriteLine("Before Where() is called."); IEnumerable<string> ieStrings = strings.Where(s => s.Length == 3); Console.WriteLine("After Where() is called."); foreach (string s in ieStrings) { Console.WriteLine("Processing " + s); } }
}
</source>