嘿嘿,其实就是一张图:
将多个输入联接到一个输出序列
class Student { public string Name { get; set; } public int Age { get; set; } public string City { get; set; } public List<int> Scores { get; set; } } class Teacher { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public string City { get; set; } }
操作代码:
public void Concat() { //创建第一个数据源 var students = new List<Student>() { new Student() { Age = 23, City = "广州", Name = "小C", Scores = new List<int>(){85,88,83,97} }, new Student() { Age = 18, City = "广西", Name = "小明", Scores = new List<int>(){86,78,85,90} }, new Student() { Age = 33, City = "梦里", Name = "小叁", Scores = new List<int>(){86,68,73,97} } }; //创建第二个数据源 var teachers = new List<Teacher>() { new Teacher() { Age = 35, City = "梦里", Name = "啵哆" }, new Teacher() { Age = 28, City = "云南", Name = "小红" }, new Teacher() { Age = 38, City = "河南", Name = "丽丽" } }; //创建我们的contact var concat = (from s in students where s.City == "新都" select s.Name ).Concat( from t in teachers where t.City == "重庆" select t.Name ); //使用我们concat将我们的两个数据集联系在一起滴呀;效果很好的; }
2.将内存中的对象转换为 XML
var students = new List<Student>() { new Student() { Age = 18, Name = "小A", Scores = new List<int>() {88,85,74,66 } }, new Student() { Age = 35, Name = "小B", Scores = new List<int>() {88,85,74,66 } }, new Student() { Age = 28, Name = "小啥", Scores = new List<int>() {88,85,74,66 } } }; //创建查询 var studentsToXml = new XElement("Root", from student in students let x = $"{student.Scores[0]},{student.Scores[1]},{student.Scores[2]},{student.Scores[3]}" select new XElement("student", new XElement("Name", student.Name), new XElement("Age", student.Age), new XElement("Scores", x)) )
这里捋一捋这个let的使用滴呀;
在Linq查询中Let子句可以创建一个新的范围变量,并使用该变量保存表达式的结
$"{radius * radius * 3.14}" 相当于 string.Format("{0}",radius * radius * 3.14),这里采用的是 C# 6.0 的语法
实例:
public static void InfoMQ() { List<Student> list = new List<Student>(){ new Student{Name="zhangs" ,Age=21}, new Student{Name="zhangs",Age=25}, new Student{Name="margy",Age=21} }; //基本用法; var query = from stu in list let b = stu.Name let a = stu.Age where b == "jack" && a > 18 select new { a, b }; //let 在linq表达式中,存储子表达式的计算结果,let创建一个范围变量,变量创建后, //实例的第二种写法; int[] numbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; //传统额子查询; var query2 = from num in numbers select num * ( from n in numbers where n % 2 == 0 select n ).Count(); //传统额做法滴呀; //接着是只用我们let的用法滴来实现滴呀; var query3 = from num in numbers let evenNumbers = (from n in numbers where n % 2 == 0 select n).Count() select num * evenNumbers; //这个就是我们的基本用法id呀; }
再加一个微软官方的实例;
-
创建一个可以查询自身的可枚举类型。
-
使查询只能对范围变量
word
调用一次ToLower
。 如果不使用let
,则必须在where
子句的每个谓词中调用ToLower
。
class LetSample1 { static void Main() { string[] strings = { "A penny saved is a penny earned.", "The early bird catches the worm.", "The pen is mightier than the sword." }; // Split the sentence into an array of words // and select those whose first letter is a vowel. var earlyBirdQuery = from sentence in strings let words = sentence.Split(' ') from word in words let w = word.ToLower() where w[0] == 'a' || w[0] == 'e' || w[0] == 'i' || w[0] == 'o' || w[0] == 'u' select word; // Execute the query. foreach (var v in earlyBirdQuery) { Console.WriteLine(""{0}" starts with a vowel", v); } // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } /* Output: "A" starts with a vowel "is" starts with a vowel "a" starts with a vowel "earned." starts with a vowel "early" starts with a vowel "is" starts with a vowel */
更多实例;http://www.3fwork.com/b101/000394MYM025538/
标准查询运算符方法的 LINQ to Objects 实现采用两种主要方式之一来执行:立即执行和延迟执行。采用延迟执行的查询运算符可以进一步分为两类:流式和非流式。
public static void FuckLife() { var words = new[] { "the", "quick", "brown", "fox", "jumps" }; //两种表达方式滴呀; var query = from w in words orderby w.Length, w.Substring(0, 1) descending select w; foreach (var word in query) { Console.WriteLine(word); } //方式二: Console.WriteLine("---"); var wordA = new[] { "the", "quick", "brown", "fox", "jumps" }; var result = wordA.OrderBy(o => o.Length).ThenByDescending(obj => obj.Substring(0, 1)); foreach (var word in result) { Console.WriteLine(word); } Console.ReadLine(); //还要用工具来查看,生成的sql语句滴呀; }
方法名 |
说明 |
C# 查询表达式语法 |
All |
确定是否序列中的所有元素都满足条件。 |
X |
Any |
确定序列中是否有元素满足条件。 |
X |
Contains |
确定序列是否包含指定的元素。 |
X |
这里有两个
运算符名称 |
说明 |
C# 查询表达式语法 |
Skip |
跳过序列中的指定位置之前的元素。 |
X |
SkipWhile |
基于谓词函数跳过元素,直到某元素不再满足条件。 |
X |
Take |
提取序列中的指定位置之前的元素。 |
X |
TakeWhile |
基于谓词函数提取元素,直到某元素不再满足条件。 | X |
这里简单提一下;skip 和take都接受的是int类型的参数;
队skipWhile 和 TakeWhile 进行简答的实例;
akeWhile:只要满足指定的条件,就会返回序列的元素
string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" }; IEnumerable<string> query = fruits.TakeWhile(fruit => String.Compare("orange", fruit, true) != 0); foreach (string fruit in query) { Console.WriteLine(fruit); } /* This code produces the following output: apple banana mango
SkipWhile:只要满足指定的条件,就跳过序列中的元素,然后返回剩余元素
int[] grades = { 59, 82, 70, 56, 92, 98, 85 }; IEnumerable<int> lowerGrades = grades .OrderByDescending(grade => grade) .SkipWhile(grade => grade >= 80); Console.WriteLine("All grades below 80:"); foreach (int grade in lowerGrades) { Console.WriteLine(grade); } /* This code produces the following output: All grades below 80: 70 59 56 */
这里我们先插播一条关于linq orderby 动态的用法,关键就是创建我们的表达式树;
// 排序条件 Expression<Func<AppraiseInfoDto, int>> oderyLamuda = null; switch (param.OrderBy) { case 1: oderyLamuda = sa => sa.Low; break; case 2: oderyLamuda = sa => sa.Min; break; case 3: oderyLamuda = sa => sa.Hight; break; default: oderyLamuda = sa => sa.Hight; break; }
然后直接将其应用在我们的lamda表达式中滴呀;
效果很好;
这里我们就简单的讲讲groupjoin,ToLookup
后面还有一些细节;