Linq Except,Distinct,Left Join

  最近项目中用到了Linq中Except,Distinct,Left Join这几个运算,这篇简单的记录一下这几种情形。

Except

     基础类型使用Linq的运算很简单,下面用来计算两个集合的差

           int[] a = {1, 2, 3, 4};


         int[] b = {2, 3, 4, 5};


         var reslut = a.Except(b);


    result 用来返回a有,b没有的值,计算结果是1。

    自定义类型实现Except

        class Employee


       {


        public int ID { get; set; }


       }


    对于引用类型,若需要根据属性进行Except运算,则看下面简单的例子

          List<Employee> employeeA = new List<Employee>() { new Employee { ID = 1 }, new Employee { ID = 2 }};


          List<Employee> employeeB = new List<Employee>() { new Employee { ID = 2 }, new Employee { ID = 3 } };


            //var reslut = employeeA.Except(employeeB);


          var reslut = (from a in employeeA


                          select a.ID).Except


                       (from b in employeeB


                           select b.ID );


    对于集合的操作,Linq中还提供了Union,Intersect等常见的几个运算,都是关系数据库中常用的运算

Distinct

     Distict是用来排除相同序列中元素的,对于基础类型,可以直接使用Distinct

       int[] a = {1, 2, 2, 3, 3, 3, 4};


       var reslut = a.Distinct();


    结果是1,2,3,4,但是对于自定义类型,则需要额外的一些操作,方式有多种,这里选择其中一种,即实现IEquatable<>

        class Employee : IEquatable<Employee>


      {


        public int ID { get; set; }


        public string Name { get; set; }


        public bool Equals(Employee other)


        {


            if (Object.ReferenceEquals(other, null))


                return false;


            if (ReferenceEquals(this, other))


                return true;


            return ID.Equals(other.ID);


        }


        public override int GetHashCode()


        {


            return ID.GetHashCode();


        }


    }


    这里重写了GetHashCode,Equals根据ID相同过滤对象,看简单的例子

        List<Employee> employees = new List<Employee>


                                           {


                                               new Employee {ID = 1, Name = "Ringgo"},


                                               new Employee {ID = 2, Name = "Rex"},


                                               new Employee {ID = 1, Name = "Ringgo"}


                                           };


        var reslut = employees.Distinct();


    这样就实现了对自定义类型的Distinct操作。    

 

Left Join


     Linq查询表达式中提供了join运算,比较常见的是join.. on ..equals,也就是内联接运算

        int[] array1 = {1, 2, 3, 4};


        int[] array2 = {1, 2,3,5};


        var reslut = from a in array1


                     join i in array2 on a equals i into  c


                     from o in c


                     select o;


    这里要说的是Left Join,这里为了方便,仅列出表达式的语法

         var result = from a in employees


                            join b in _biddingBase


                            on a.EmployeeId equals b.EmployeeId into  temp


                            from t in temp.DefaultIfEmpty()


                            select new 


                            {


                                Name = a.Name,


                                OrganizationName = a.OrganizaionName,


                                EmployeeId = a.AgentId,


                                Id = a.EmployeeId,


                                OrganizationId = a.OrganizaionId,


                                DayOffValue  = t==null?0:t.DayOffValue,


                                TimeOffValue = t==null?0:t.TimeOffValue


                            }; 


    这段代码主要是用到DefaultIfEmpty(),那么在右集合为null时,注意DayOffValue = t==null?0:t.DayOffValue这种语法即可。

    如果你对数据库的表的各种联接运算理解的话,我想用LINQ这种语法会更方便。

    以上即是这段时间项目中遇到的一些问题,为了之后再遇到这种问题时不必花时间,这里自己把这些问题提取出来,也希望对你有所帮助。

原文地址http://www.fengfly.com/plus/view-196868-1.html

 

posted @ 2013-02-21 16:32  南潇湘  阅读(468)  评论(0编辑  收藏  举报