Program.cs代码如下:
class Program
{
private static void Main(string[] args)
{
//SelectMany();
//Select_SelectMany_Difference();
//Aggregate();
//How_Long_Entire_Album_Is();
//Expand_Range();
//Expand_Range_In_Order_No_Dups();
Search_Log_Files();
Console.ReadKey();
}
public static void SelectMany()
{
string[] array = { "dot", "net", "perls" };
// 转换 字符数组中的每个字符串 为 单字符数组。
// 然后将这些字符数组合二为一.
var result1 = array.SelectMany(element => element.ToCharArray());
// 显示字母
foreach (char letter in result1)
{
Console.WriteLine(letter);
}
// 使用像cross join 在 SQL
List<int> number = new List<int>() { 10, 20 };
List<string> animals = new List<string>() { "cat", "dog", "donkey" };
List<string> guojia = new List<string>() { "中国", "美国","日本" };
List<string> citys = new List<string>() { "风", "火", "雷", "电", "气", "金", "木","水", "土" };
//摘要:
//将序列的每个元素投影到 System.Collections.Generic.IEnumerable<T>,并将结果序列合并为一个序列,并对其中每个元素调用结果选择器函数。
//类型参数:
//TSource: source 中的元素的类型。
//TCollection: collectionSelector 收集的中间元素的类型。
//TResult: 结果序列的元素的类型。
//参数:
//source: 一个要投影的值序列。
//collectionSelector: 一个应用于输入序列的每个元素的转换函数。
//resultSelector: 一个应用于中间序列的每个元素的转换函数。
var result2 = number.SelectMany(num => animals, (n, a) => new { n, a });
foreach (var resultItem in result2)
{
Console.WriteLine("{0}{1}", resultItem.n, resultItem.a);
}
var resultGC = guojia.SelectMany(num => citys, (n, a) => new { n, a });
foreach (var resultItem in resultGC)
{
Console.WriteLine("{0}{1}", resultItem.n, resultItem.a);
}
}
public static void Select_SelectMany_Difference()
{
//合并多个集合到一个
Entities[] petOwners =
{
new Entities {Name = "Higa, Sidney", Pets = new List<string> {"Scruffy", "Sam"}},
new Entities {Name = "Ashkenazi, Ronen", Pets = new List<string> {"Walker", "Sugar"}},
new Entities {Name = "Price, Vernette", Pets = new List<string> {"Scratches", "Diesel"}}
};
IEnumerable<List<String>> result1 = petOwners.Select(petOwner => petOwner.Pets);
//注意到两个foreach循环都需要通过结果来迭代,因为查询都返回集合。
foreach (List<String> petList in result1)
{
foreach (string pet in petList)
{
Console.WriteLine(pet);
}
}
Console.WriteLine("----------------------------------------------------------");
IEnumerable<string> result2 = petOwners.SelectMany(petOwner => petOwner.Pets);
// 只有一个foreach循环需要迭代,因为它的结果是一维集合。
foreach (string pet in result2)
{
Console.WriteLine(pet);
}
}
public static void Aggregate()
{
int[] array = { 1, 2, 3, 4, 5 };
int result = array.Aggregate((a, b) => b + a);
// 1 + 2 = 3
// 3 + 3 = 6
// 6 + 4 = 10
// 10 + 5 = 15
Console.WriteLine(result);
int result1 = array.Aggregate((a, b) => b * a);
// 1 * 2 = 2
// 2 * 3 = 6
// 6 * 4 = 24
// 24 * 5 = 120
Console.WriteLine(result1);
string sentence = "the quick brown fox jumps over the lazy dog";
//拆分字符串为单个单词。
string[] words = sentence.Split(' ');
//预先准备每个单词到新句子的开头来扭转词序。
//参数:
//source: 要聚合的 System.Collections.Generic.IEnumerable<T>。
//func: 要对每个元素调用的累加器函数。
//返回值: 累加器的最终值。
string result3 = words.Aggregate((workingSentence, next) => next + " " + workingSentence);
Console.WriteLine(result3);
}
public static void How_Long_Entire_Album_Is()
{
var trackTimes = "2:54,3:48,4:51,3:32,6:15,4:08,5:17,3:13,4:16,3:55,4:53,5:35,4:24";
var totalTrackTimesInSeconds =
trackTimes.Split(',')
.Select(t => "0:" + t)
.Select(t => TimeSpan.Parse(t))
.Select(t => t.TotalSeconds)
.Sum();
var totalTrackTimes = TimeSpan.FromSeconds(totalTrackTimesInSeconds);
Console.WriteLine(totalTrackTimes);
Console.WriteLine(trackTimes.Split(',').Select(t => TimeSpan.Parse("0:" + t)).Aggregate((t1, t2) => t1 + t2));
}
public static void Expand_Range()
{
// 扩大范围
// 例如,“2,3-5,7”应该扩大到2,3,4,5,7
var data = "2,5,7-10,11,17-18";
var result = data
.Split(',')
.Select(x => x.Split('-'))
.Select(p => new { First = int.Parse(p[0]), Last = int.Parse(p.Last()) })
.SelectMany(r => Enumerable.Range(r.First, r.Last - r.First + 1));
Console.WriteLine(result);
}
public static void Expand_Range_In_Order_No_Dups()
{
// 扩大范围
// 例如,“2,3-5,7”应该扩大到2,3,4,5,7
// "6,1-3,2-4" 应该扩大到 1,2,3,4,6
var data = "6,1-3,2-4";
var result = data
.Split(',')
.Select(x => x.Split('-'))
.Select(p => new { First = int.Parse(p[0]), Last = int.Parse(p.Last()) })
.SelectMany(r => Enumerable.Range(r.First, r.Last - r.First + 1))
.OrderBy(r => r)
.Distinct();
}
public static void Search_Log_Files()
{
//指定用于检索系统特殊文件夹的目录路径的枚举常数。
var myDocs = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var diagsFolder = Path.Combine(myDocs, "QQ", "tec");
var fileType = "*.txt";
var searchTerm = "JSONArray";
var result = Directory.EnumerateFiles(diagsFolder, fileType).SelectMany(file => File.ReadAllLines(file,Encoding.UTF8)
.Select((line, index) => new
{
File = file,
Text = line,
LineNumber = index + 1
})).Where(line => Regex.IsMatch(line.Text, searchTerm));
Console.WriteLine(result.FirstOrDefault().Text);
}
}