zoukankan      html  css  js  c++  java
  • <收录>Linq :Aggregate;SequenceEqual;join

    Linq :Aggregate;SequenceEqual;join
    1. Aggregate(使用 Aggregate 创建数组的连乘,计算所有元素的总乘积。):
    
      double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };
                
          double product = doubles.Aggregate((runningProduct, nextFactor) => runningProduct * nextFactor);
    
    2. Aggregate重载(使用 Aggregate 创建一个流水账余额, 从最初余额 100 减去每次取出的金额,直到余额减少到 0 以下为止。):
    
        double startBalance = 100.0;
                
                int[] attemptedWithdrawals = { 20, 10, 40, 50, 10, 70, 30 };
                
                double endBalance = 
                    attemptedWithdrawals.Aggregate(startBalance,
                        (balance, nextWithdrawal) =>
                            ( (nextWithdrawal <= balance) ? (balance - nextWithdrawal) : balance ) );
    
    3.  SequenceEqual(使用 SequenceEquals 查看两个序列中所有元素是否以相同顺序匹配。):
    
        var wordsA = new string[] { "cherry", "apple", "blueberry" };
                var wordsB = new string[] { "cherry", "apple", "blueberry" };
                
                bool match = wordsA.SequenceEqual(wordsB);
    
    4. SequenceEqual重载(自定义比较方法):
    
      List<Student> list = new List<Student>();
    
      List<Student> list2 = new List<Student>();
    
      Student a = new Student
        {
             UserId = 1,
             StudentName = "Eric"
    
        };
        Student b = new Student
        {
             UserId = 1,
             StudentName = "Eric"
    
        };
    
        Student c = new Student
        {
             UserId = 2,
             StudentName = "laoyi"
    
        };
        list.Add(a);
        list.Add(b);
    
        list.Add(c);
    
        list2.Add(c);
        list2.Add(b);
    
        list2.Add(a);
    
        var tt = list.SequenceEqual(list, new StudentComparer());
     
    
      public class Student
      {
        public int UserId { get; set; }
        public string StudentName { get; set; }
      }
     
    
      自定义的比较类:
      public class StudentComparer : IEqualityComparer<Student>
      {
        public bool Equals(Student x, Student y)
        {
               return x.UserId.Equals(y.UserId);
        }
    
        public int GetHashCode(Student obj)
        {
               return obj.UserId.GetHashCode();
        }
      }
    
     5. join in (左外部联接和复合键,使用匿名类型封装多个键值):
    
         List<Customer> customers = GetCustomerList();
                List<Supplier> suppliers = GetSupplierList();
    
                var supplierCusts =
                    from sup in suppliers
                    join cust in customers on new { sup.City, sup.Country } equals new { cust.City, cust.Country } into cs
                    from c in cs.DefaultIfEmpty() //移除 DefaultIfEmpty 方法调用可以使之成为内部联接
                    orderby sup.SupplierName
                    select new { Country = sup.Country, 
                                 City = sup.City,
                                 SupplierName = sup.SupplierName,
                                 CompanyName = c == null ? "(No customers)" : c.CompanyName
                               };
    
     
  • 相关阅读:
    字符串匹配算法
    C#中窗体的位置和大小
    关于C#值类型,引用类型,值传递,引用传递(转载)
    ArcMap中设置.mxd相对路径
    统计学上的知识
    .NET 数学实现资料(ZZ)
    牛腩新闻系统学习笔记06讲 编写SQLHelper
    DropDownList 控件不能触发SelectedIndexChanged 事件的另一个原因
    牛腩新闻视频 03讲 数据库设计的心得 如何建立外键sql2008的数据库关系图功能
    使用sql server management studio 2008 连接数据库,无法查看数据库,提示 无法为该请求检索数据 错误916
  • 原文地址:https://www.cnblogs.com/wzq806341010/p/3077317.html
Copyright © 2011-2022 走看看