zoukankan      html  css  js  c++  java
  • GroupBy之后加ToList和不加ToList有什么区别吗?

        class Program
        {
            static void Main(string[] args)
            {
                List<Person> people = new List<Person>() {
                    new Person(1,12),
                     new Person(1,24),
                      new Person(1,26)
                };
                var pers = people.GroupBy(p => p.Id)
                    .Select(p => new Person()
                    {
                        Id = p.Key,
                        Price = p.Sum(a => a.Price)
                    }).ToList();//ToList了后面people 里面再加新的元素不会影响 到pers的值,已经指向了新的地址
                var pers1 = people.GroupBy(p => p.Id)
                 .Select(p => new Person()
                 {
                     Id = p.Key,
                     Price = p.Sum(a => a.Price)
                 });//people加新的元素之后,pers1的值会随之改变,因为指向的是同一地址
                people.Add(new Person(1,20));
                Console.WriteLine("加了ToList之后的pers的总和为{0}",pers.FirstOrDefault().Price);
                Console.WriteLine("没加ToList的pers1的总和为{0}", pers1.FirstOrDefault().Price);
            }
          
        }
        public class Person
        {
            private int _id;
            private decimal _price;
            public int Id { get; set; }
            public decimal Price { get; set; }
            public Person()
            {
            }
            public Person(int _id,decimal _price)
            {
                Id = _id;
                Price = _price;
            }
        }
     
  • 相关阅读:
    javaEE项目部署方式
    Docker安装mysql5.6
    自定义镜像mycentos
    DockerFile体系结构(保留字指令)
    Cognition math based on Factor Space (2016.05)
    MATLAB画ROC曲线,及计算AUC值
    MATLAB时间序列预测Prediction of time series with NAR neural network
    因素空间发展评述
    ps 证件照(1,2寸)
    kali linux wmtools安装
  • 原文地址:https://www.cnblogs.com/xiaoxinstart/p/11973403.html
Copyright © 2011-2022 走看看