你的位置:首页 > 软件开发 > ASP.net > 刚刚接触的LINQ

刚刚接触的LINQ

发布时间:2015-06-14 00:00:59
科普一下:语言集成查询(Language INtegrated Query,LINQ)是一项微软技术,新增一种自然查询的SQL语法到.NET Framework的编程语言中,可支持Visual Basic .NET以及C#语言。 LINQ定义了大约40个查询操作符,如sele ...

科普一下:

语言集成查询(Language INtegrated Query,LINQ)

是一项微软技术,新增一种自然查询的SQL语法到.NET Framework的编程语言中,可支持Visual Basic .NET以及C#语言。

 

LINQ定义了大约40个查询操作符,如select、from、in、where以及order by(C#中)。

使用这些操作符可以编写查询语句。不过,这些查询还可以基于很多类型的数据,每个数据类型都需要一个单独的LINQ类型。

基础语法:

  • LINQ的关键词:from, select, in, where, group by, orderby, …
  • LINQ的注意点:必须以select或者是group by 结束。
  • LINQ的写法:

1)from 临时变量 in 实现IEnumerable<T>接口的对象

where条件表达式

[orderby条件]

[group by 条件]

select 临时变量中被查询的值

2) 实现IEnumerable接口的对象.LINQ方法名(lambda表达式)。如:

get='_blank'>string input = "hellow world";

int count = input.Count(w=>w == 'o'); //查询字母o出现的次数

能够使用LINQ的对象需要实现IEnumerable<T>接口。并且LINQ的查询表达式是在一次创建对象时才被编译的。

  • LINQ的全称:Language-Integrated Query
  • 命名空间:System.Linq;

  注意:Linq是在.NET Framework 3.5 中出现的技术,所以在创建新项目的时候必须要选3.5或者更高版本,否则无法使用。

  选择3.5或更高版本的.NET Framework之后,创建的新项目中会自动包含System.Linq的命名空间。

接着看代码实现:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace LINQ{  public class Category  {    public int Age { get; set; }    public string Prase { get; set; }  }  public class Program  {    static void Main(string[] args)    {      //对数据集合的操作      List<string> names = new List<string> { "Tom", "Jack", "Jim", "Jackson", "Key", "Kitty" };      var nameJ = from n in names where n.StartsWith("J") orderby n select n;      foreach (var name in nameJ)      {        Console.WriteLine(name);      }      Console.WriteLine("-------------------------");      names.Add("Jack1");      names.Add("Jack2");      names.Add("Jack3");      foreach (string name in nameJ)      {        Console.WriteLine(name);      }      Console.WriteLine("-------------------------");            List<Category> cs = new List<Category>() {         new Category{Age=22,Prase="13期"},new Category{Age=22,Prase="12期"},new Category{Age=22,Prase="14期"},      new Category{Age=23,Prase="14期"},new Category{Age=20,Prase="13期"},new Category{Age=24,Prase="14期"},      new Category{Age=25,Prase="14期"},new Category{Age=23,Prase="13期"},new Category{Age=25,Prase="14期"}      };      var s = (from s1 in cs where s1.Age > 21 && s1.Prase == "14期" orderby s1.Age descending select s1).Take(3);      foreach (var c1 in s)      {        Console.WriteLine(c1.Age + " " + c1.Prase);      }      Console.WriteLine("-------------------------");      var ss = from s1 in cs          group s1 by s1.Prase into c          orderby c.Count() descending, c.Key          where c.Count() >= 2          select c.Key + c.Count();      foreach (var s1 in ss)      {        Console.WriteLine(s1);      }      Console.WriteLine("-------------------------");      object[] data = { "hello",33,"what",36,"fine",39,"thanks"};      var str = data.OfType<string>();      foreach (var a in str)      {        Console.WriteLine(a);      }      Console.ReadKey();    }  }}

函数支持:


原标题:刚刚接触的LINQ

关键词:linq

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。