- from where select`
static void Main(string[] args)
{
int[] numbers = { 2,12,5,15};
IEnumerable<int> lowNums = from n in numbers
where n < 10
select n;
foreach (int x in lowNums)
Console.WriteLine("{0}", x);
Console.ReadLine();
}
输出:2,5
- from join where select
public class Student // Declare classes.
{
public int StID;
public string LastName;
}
public class CourseStudent
{
public string CourseName;
public int StID;
}
static Student[] students = new Student[] {
new Student { StID = 1, LastName = "Carson" },
new Student { StID = 2, LastName = "Klassen" },
new Student { StID = 3, LastName = "Fleming" },
};
// Initialize arrays.
static CourseStudent[] studentsInCourses = new CourseStudent[] {
new CourseStudent { CourseName = "Art", StID = 1 },
new CourseStudent { CourseName = "Art", StID = 2 },
new CourseStudent { CourseName = "History", StID = 1 },
new CourseStudent { CourseName = "History", StID = 3 },
new CourseStudent { CourseName = "Physics", StID = 3 },
};
static void Main()
{
// Find the last names of the students taking history.
var query = from s in students
join c in studentsInCourses on s.StID equals c.StID
where c.CourseName == "History"
select s.LastName;
// Display the names of the students taking history.
foreach ( var q in query )
Console.WriteLine( "Student taking History: {0}", q );
}
输出:Student taking History:Carson
Student taking History:Fleming
- from from where select
static void Main()
{
var groupA = new[] { 3, 4, 5, 6 };
var groupB = new[] { 6, 7, 8, 9 };
var someInts = from a in groupA
from b in groupB
where a > 4 && b <= 8
select new { a, b, sum = a + b };
foreach ( var a in someInts )
Console.WriteLine( a );
}
输出:{a=5,b=6,sum=11}
{a=5,b=7,sum=12}
{a=5,b=8,sum=13}
{a=6,b=6,sum=12}
{a=6,b=7,sum=13}
{a=6,b=8,sum=14}
- from let where select
static void Main()
{
var groupA = new[] { 3, 4, 5, 6 };
var groupB = new[] { 6, 7, 8, 9 };
var someInts = from a in groupA
from b in groupB
let sum = a + b
where sum == 12
select new { a, b, sum };
foreach ( var a in someInts )
Console.WriteLine( a );
}
输出:{a=3,b=9,sum=11}
{a=4,b=8,sum=12}
{a=5,b=7,sum=13}
{a=6,b=6,sum=12}
- 多个where子句条件
static void Main()
{
var groupA = new[] { 3, 4, 5, 6 };
var groupB = new[] { 6, 7, 8, 9 };
var someInts = from int a in groupA
from int b in groupB
let sum = a + b
where sum >= 11
where a == 4
select new { a, b, sum };
foreach ( var a in someInts )
Console.WriteLine( a );
}
输出:{a=4,b=7,sum=11}
{a=4,b=8,sum=12}
{a=4,b=9,sum=13}
- from orderby select
static void Main()
{
var students = new[] // Array of objects of an anonymous type
{
new { LName="Jones", FName="Mary", Age=19, Major="History" },
new { LName="Smith", FName="Bob", Age=20, Major="CompSci" },
new { LName="Fleming", FName="Carol", Age=21, Major="History" }
};
var query = from student in students
orderby student.Age
select student;
foreach ( var s in query )
{
Console.WriteLine( "{0}, {1}: {2} - {3}",
s.LName, s.FName, s.Age, s.Major );
}
}
输出:Jones,Mary: 19-History
Smith,Bob, 20-CompSci
Fleming,Carol, 21-History
- from group by
static void Main(string[] args)
{
var students = new[]
{
new {Lname="Jones",Fname="Mary",Age=19,Major="History"},
new {Lname="Smnith",Fname="Bob",Age=20,Major="CompSic"},
new {Lname="Fleming",Fname="Carol",Age=21,Major="History"},
};
var query = from student in students
group student by student.Major;
foreach (var s in query)
{
Console.WriteLine("{0}", s.Key);
foreach (var t in s)
Console.WriteLine(" {0},{1}", t.Lname, t.Fname);
}
Console.ReadLine();
}
输出:History
Jones,Mary
CompSci
Smith,Bob
- 查询延续:into子句
static void Main()
{
var groupA = new[] { 3, 4, 5, 6 };
var groupB = new[] { 4, 5, 6, 7 };
var someInts = from a in groupA
join b in groupB on a equals b
into groupAandB
from c in groupAandB
select c;
foreach ( var a in someInts )
Console.Write( "{0} ", a );
}
输出:4 5 6
源码来源于《C#图解教程》