欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linq

程序员文章站 2022-04-15 14:45:27
...

linq是什么?
Language Integrated Query语言集成查询,他是集成在.NET编程语言中的一种特性,Linq体系结构如下,包括3层架构,他不仅可以操作sql数据,还可以操作内存数据,如Object ,Entity ,XML 等。但是linq 对外提供统一的接口
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 = "aaa@qq.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决定,他封装了对各个数据源的操作。这只是其中一部分,理解的还不够透彻,望多多交流。