none
执行这一命令后为什么源数据也变了 RRS feed

  • 问题

  •         class Package
            {
                public int ID { get; set; }
                public string Company { get; set; }

                public double Weight { get; set; }
            }

    List<Package> packages = new List<Package>
                {
                    new Package { ID = 1, Company = "Coho Vineyard", Weight = 25.2 },
                    new Package { ID = 1, Company = "Lucerne Publishing", Weight = 18.7 },
                    new Package { ID = 2, Company = "Wingtip Toys", Weight = 6.0 },
                    new Package { ID = 2, Company = "Adventure Works", Weight = 33.8 }
                };

                double totalWeight1 = packages.Sum(pkg => pkg.Weight);

                var row0 = from package in packages
                           group package by package.ID = 1 into g
                           orderby g.Key
                           select new
                           {
                               ID = g.Key,
                               Weight = g.Sum(c => c.Weight)                          
                           };

                double totalWeight2 = row0.Sum(c => c.Weight);

     为什么执行最后一个语句结束后,packages中的ID全部变成了1 ,哪里的问题啊,该怎么做才行?


    • 已编辑 blllue 2015年1月21日 9:40
    2015年1月21日 9:19

答案

  • Hello,

    >> 为什么执行最后一个语句结束后,packages中的ID全部变成了1 ,哪里的问题啊,该怎么做才行?

    groupby的时候你给所有的记录重新赋值了:

    group package by package.ID = 1 into g

    去掉“= 1”,如果你是想对ID1 的行分组,请用where语法:

    var row0 = from package in packages
    
                               where package.ID == 1 //删选ID为1的行
    
                               group package by package.ID into g
    
                               orderby g.Key
    
                               select new
    
                               {
    
                                   ID = g.Key,
    
                                   Weight = g.Sum(c => c.Weight)
    
                               };

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2015年1月22日 2:35
    版主

全部回复

  • Hello,

    >> 为什么执行最后一个语句结束后,packages中的ID全部变成了1 ,哪里的问题啊,该怎么做才行?

    groupby的时候你给所有的记录重新赋值了:

    group package by package.ID = 1 into g

    去掉“= 1”,如果你是想对ID1 的行分组,请用where语法:

    var row0 = from package in packages
    
                               where package.ID == 1 //删选ID为1的行
    
                               group package by package.ID into g
    
                               orderby g.Key
    
                               select new
    
                               {
    
                                   ID = g.Key,
    
                                   Weight = g.Sum(c => c.Weight)
    
                               };

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2015年1月22日 2:35
    版主
  • 确实如此,嘻嘻,谢谢老师
    2015年1月22日 13:09