Linq

linq是什么?
Language Integrated Query语言集成查询,他是集成在.NET编程语言中的一种特性,Linq体系结构如下,包括3层架构,他不仅可以操作sql数据,还可以操作内存数据,如Object ,Entity ,XML 等。但是linq 对外提供统一的接口
这里写图片描述

为什么要使用LINQ技术?
1.查询是一种从数据源检索数据的表达式,查询通道采用专门的查询语言,随着技术的发展,针对不同的数据源开发了不同的查询语言,用于关系数据库的SQL语言,用于XML的Xquery等。为了实现数据的查询,并跟上技术的进步,开发人员不得不针对特定查询语言所支持的数据源或数据格式,而花费大量时间学习如何使用它们。

但是linq查询中,始终会用到对象,不管用户是操作xml,还是object,entity等,都可以使用统一的查询方式,linq 把查询和设置等操作封装起来了,他自己判断是执行sql 还是xml.

LINQ中关键字:
select查询:

List<string> words = new List<string>() { "an", "apple", "a", "day" };

        var query = from word in words
                    select word.Substring(0, 1);

select 投影

 var persons = GetPersons();

    var query = from p in persons
                select p.Name;

    foreach (var item in query)
    {
        Console.WriteLine(item);
    }

where过滤:

string[] words = { "humpty", "dumpty", "set", "on", "a", "wall" };

        IEnumerable<string> query = from word in words
                                    where word.Length == 3
                                    select word;
        foreach (string str in query)
           Console.WriteLine(str);
           Console.ReadLine();            

join连接:

List <DepartmentClass> departments = new List <DepartmentClass>();
              departments.Add(new DepartmentClass { DepartmentId = 1, Name = "Account" });
              departments.Add(new DepartmentClass { DepartmentId = 2, Name = "Sales" });
              departments.Add(new DepartmentClass { DepartmentId = 3, Name = "Marketing" });

        List <EmployeeClass> employees = new List <EmployeeClass>();
              employees.Add(new EmployeeClass { DepartmentId = 1, EmployeeId = 1, EmployeeName = "William" });
              employees.Add(new EmployeeClass { DepartmentId = 2, EmployeeId = 2, EmployeeName = "Miley" });
              employees.Add(new EmployeeClass { DepartmentId = 1, EmployeeId = 3, EmployeeName = "Benjamin" });


        var list = (from e in employees
                   join d in departments on e.DepartmentId equals d.DepartmentId
                   select new
                   {
                       EmployeeName = e.EmployeeName,
                       DepartmentName = d.Name
                   });

orderby排序:

int[] num = { -20, 12, 6, 10, 0, -3, 1 }
    var posNums = from n in num
                  orderby n
                  select n;

group by 分组:

List<int> numbers = new List<int>() { 35, 44, 200, 84, 3987, 4, 199, 329, 446, 208 };

IEnumerable<IGrouping<int, int>> query = from number in numbers

group number by number % 2;

Enumerable.Except去重:

double[] numbers1 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 };
        double[] numbers2 = { 2.2 };

        IEnumerable<double> onlyInFirstSet = numbers1.Except(numbers2);

        foreach (double number in onlyInFirstSet)
           Console.WriteLine(number);
           Console.ReadLine();
     }

Enumerable.SequenceEqual相等:

        Pet barley = new Pet() { Name = "Barley", Age = 4 };
        Pet boots = new Pet() { Name = "Boots", Age = 1 };
        Pet whiskers = new Pet() { Name = "Whiskers", Age = 6 };

        List<Pet> pets1 = new List<Pet>() { barley, boots };
        List<Pet> pets2 = new List<Pet>() { barley, boots };
        List<Pet> pets3 = new List<Pet>() { barley, boots, whiskers };

        bool equal = pets1.SequenceEqual(pets2);
        bool equal3 = pets1.SequenceEqual(pets3);

        Console.WriteLine("The lists pets1 and pets2 {0} equal.", equal ? "are" :"are not");
        Console.WriteLine("The lists pets1 and pets3 {0} equal.", equal3 ? "are" :"are not");

linq to sql:

 string connectString = System.Configuration.ConfigurationManager.ConnectionStrings["LinqToSQLDBConnectionString"].ToString();

        LinqToSQLDataContext db =new LinqToSQLDataContext(connectString);           


        Employee newEmployee = new Employee();
        newEmployee.Name = "Michael";
        newEmployee.Email = "yourname@companyname.com";
        newEmployee.ContactNo = "343434343";
        newEmployee.DepartmentId = 3;
        newEmployee.Address = "Michael - USA";

        db.Employees.InsertOnSubmit(newEmployee);

        db.SubmitChanges();

linq to object:

List<Department> departments = new List<Department>();
        departments.Add(new Department { DepartmentId = 1, Name = "Account" });
        departments.Add(new Department { DepartmentId = 2, Name = "Sales" });
        departments.Add(new Department { DepartmentId = 3, Name = "Marketing" });

        var departmentList = from d in departments
                             select d;

linq to entity:

using (LinqToSQLDBEntities context = new LinqToSQLDBEntities())
        {
           var departmentList = from d in context.Departments
           select d;

总结:
linq中把所有的操作都当成对象来操作,在底层是对不同数据源的操作,但是对外有统一的接口,至于决定是哪个数据源的事情由linq决定,他封装了对各个数据源的操作。这只是其中一部分,理解的还不够透彻,望多多交流。

评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诗琪小姐姐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值