LINQ 是什么? LINQPad 下载
LINQ,语言集成查询(Language INtegrated Query)是一组用于c#和Visual Basic语言的扩展。它允许编写C#或者Visual Basic代码以查询数据库相同的方式操作内存数据。
LINQ是一种用来进行数据访问的编程模型,使得.NET语言可以直接支持数据查询。LINQ的目标是降低访问数据的复杂度。LINQ可以用统一的方法访问不同类型的数据,可以将数据作为对象使用,能够更好地与编成模型集成,可以在Visual Studio中进行智能提示。
LINQ可以为SQL Server提供对象到关系的映射。此外,LINQ可以将单个类映射到多个表或视图,可以进行存储查询和实体查询。
要学好LINQ查询语法,就不得不先理解C# 3.0的一些新特性:
隐含类型局部变量
var age = 26;
var username = "zhuye";
var userlist = new [] {"a","b","c"};
foreach(var user in userlist)
Console.WriteLine(user);
纯粹给懒人用的var关键字,告诉编译器(对于CLR来说,它是不会知道你是否使用了var,苦力是编译器出的),你自己推断它的类型吧,我不管了。但是既然让编译器推断类型就必须声明的时候赋值,而且不能是null值。注意,这只能用于局部变量,用于字段是不可以的。
匿名类型
var data = new {username = "zhuye",age = 26};
Console.WriteLine("username:{0} age:{1}", data.username, data.age);
匿名类型允许开发人员定义行内类型,无须显式定义类型。常和var配合使用,var用于声明匿名类型。定义一个临时的匿名类型在LINQ查询句法中非常常见,我们可以很方便的实现对象的转换和投影。
扩展方法
public static class helper
{
public static string MD5Hash(this string s)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s,"MD5");
}
public static bool In(this object o, IEnumerable b)
{
foreach(object obj in b)
{
if(obj==o)
return true;
}
return false;
}
}
// 调用扩展方法
Console.WriteLine("123456".MD5Hash());
Console.WriteLine("1".In(new[]{"1","2","3"}));
很多时候我们需要对CLR类型进行一些操作,苦于无法扩展CLR类型的方法,只能创建一些helper方法,或者生成子类。扩展方法使得这些需求得意实现,同时也是实现LINQ的基础。定义扩展方法需要注意,只能在静态类中定义并且是静态方法,如果扩展方法名和原有方法名发生冲突,那么扩展方法将失效。、
自动属性
public class Person
{
public string username { get; protected set; }
public int age { get; set; }
public Person()
{
this.username = "zhuye";
}
}
Person p = new Person();
//p.username = "aa";
Console.WriteLine(p.username);
意义不是很大,纯粹解决机械劳动。编译器自动为你生成get、set操作以及字段,并且你不能使用字段也不能自定义get、set操作,不过你可以分别定义get和set的访问级别。
对象初始化器
public class Person
{
public string username { get; set; }
public int age { get; set; }
public override string ToString()
{
return string.Format("username:{0} age:{1}", this.username, this.age);
}
}
Person p = new Person() {username = "zhuye", age=26};
Console.WriteLine(p.ToString());
编译器会自动为你做setter操作,使得原本几行的属性赋值操作能在一行中完成。这里需要注意:
允许只给一部分属性赋值,包括internal访问级别
可以结合构造函数一起使用,并且构造函数初始化先于对象初始化器执行
集合初始化器
public class Person
{
public string username { get; set; }
public int age { get; set; }
public override string ToString()
{
return string.Format("username:{0} age:{1}", this.username, this.age);
}
}
var persons = new List {
new Person {username = "a", age=1},
new Person {username = "b", age=2}};
foreach(var p in persons)
Console.WriteLine(p.ToString());
编译器会自动为你做集合插入操作。如果你为Hashtable初始化的话就相当于使用了两个对象初始化器。
Lambda表达式
var list = new [] { "aa", "bb", "ac" };
var result = Array.FindAll(list, s => (s.IndexOf("a") > -1));
foreach (var v in result)
Console.WriteLine(v);
其实和2.0中的匿名方法差不多,都是用于产生内联方法,只不过Lambda表达式的语法更为简洁。语法如下:
(参数列表) => 表达式或者语句块
其中:
参数个数:可以有多个参数,一个参数,或者无参数。
表达式或者语句块:这部分就是我们平常写函数的实现部分(函数体)。
前面的示例分别是1个参数的例子,下面结合扩展方法来一个复杂的例子:
public delegate int mydg(int a, int b);
public static class LambdaTest
{
public static int oper(this int a, int b, mydg dg)
{
return dg(a, b);
}
}
Console.WriteLine(1.oper(2, (a, b) => a + b));
Console.WriteLine(2.oper(1, (a, b) => a - b));
查询句法
var persons = new List {
new Person {username = "a", age=19},
new Person {username = "b", age=20},
new Person {username = "a", age=21},
};
var selectperson = from p in persons where p.age >= 20 select p.username.ToUpper();
foreach(var p in selectperson)
Console.WriteLine(p);
查询句法是使用标准的LINQ查询运算符来表达查询时一个方便的声明式简化写法。该句法能在代码里表达查询时增进可读性和简洁性,读起来容易,也容易让人写对。Visual Studio 对查询句法提供了完整的智能感应和编译时检查支持。编译器在底层把查询句法的表达式翻译成明确的方法调用代码,代码通过新的扩展方法和Lambda表达式语言特性来实现。上面的查询句法等价于下面的代码:
var selectperson = persons.Where(p=>p.age>=20).Select(p=>p.username.ToUpper());
LINQ查询句法可以实现90%以上T-SQL的功能(由于T-SQL是基于二维表的,所以LINQ的查询语法会比T-SQL更简单和灵活),但是由于智能感应的原因,select不能放在一开始就输入。
从技术角度而言,LINQ定义了大约40个查询操作符,如select、from、in、where以及order by(C#中)。使用这些操作符可以编写查询语句。不过,这些查询还可以基于很多类型的数据,每个数据类型都需要一个单独的LINQ类型。
Tired of querying in antiquated SQL?
下载地址: http://www.linqpad.net/
Well, you don't have to! LINQPad lets you interactively query databases in a modern query language: LINQ. Kiss goodbye to SQL Management Studio!
LINQPad supports everything in C# 5.0 and Framework 4.x:
- LINQ to Objects
- LINQ to SQL, Entity Framework
- LINQ to XML
- Parallel LINQ
- OData / WCF Data Services, SharePoint, and Windows DataMarket
- Microsoft's ubercool Reactive Extensions
And that's not all - there's also specific support for:
- SQL Azure, SQL Table Storage, Oracle, SQLite and MySQL
- Microsoft StreamInsight
- Microsoft Dynamics CRM
- Third-party ORMs including Mindscape LightSpeed, DevArt's LinqConnect, LLBLGen,DevExpress eXpress Persistent Objects and DevForce
- (Even old-fashioned SQL!)
LINQPad is also a great way to learn LINQ: it comes loaded with 500 examples from the book, C# 5.0 in a Nutshell. There's no better way to experience the coolness of LINQ andfunctional programming.
LINQ is just the beginning
Querying is merely a special case of using LINQPad. More generally, LINQPad is aC#/VB/F# scratchpad that instantly executes any expression, statement block or program with rich output formatting and a wealth of features that "just work". Put an end to those hundreds of Visual Studio Console projects cluttering your source folder and join the revolution of LINQPad scripters, testers, and incremental developers. Reference your own assemblies and NuGet packages. Script in your favorite .NET language with optional autocompletion and the entire .NET Framework at your fingertips. Experience the magic of dynamic development and instant feedback in the ultimate programmer's playground!
Free and light
Best of all, LINQPad standard edition is free and doesn't slow down your computer when you install it! (There's even a standalone executable, and a version that runs in Google Chrome.) The program is under 5MB and is self-updating.
Prerequisites
LINQPad requires .NET Framework 4.0/4.5 (or Framework 3.5 for LINQPad 2.x).
If you have Visual Studio or Visual C# Express, the .NET Framework will already be installed.
License
LINQPad standard edition is free to download and use. Autocompletion is an optional extra.
LINQPad is not an open-source product and the source code is protected by standard copyright laws. Nonetheless, you are free to disassemble the executable to satisfy your curiosity. The author provides no warranties, and accepts no liability for direct or consequential damages.Read full EULA