Linq方法语法和查询语法

  1. 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

  1. 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

  1. 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}

  1. 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}

  1. 多个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}

  1. 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

  1. 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

  1. 查询延续: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#图解教程》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值