日期:2014-05-20  浏览次数:20802 次

linq 去除重复 和 得到某一列数据的和
C# code

 private void GvDataBind()
        {
            
            var contracts = from c in db.B_Contract_Out                            
                            join h in db.B_Hospital on c.HospitalID equals h.ID into ch                            
                            where c.C4 == "1"
                            from chs in ch.DefaultIfEmpty() 
                            join u in db.sys_User on c.PrincipalUser equals u.UserID into cu                            
                            from cus in cu.DefaultIfEmpty()
                            orderby c.ContractDate descending
                            join p in db.B_PayRecord on c.ID equals p.ContractID into cp
                            from cps in cp.DefaultIfEmpty()
                            where c.AllMoney != Convert.ToDouble(cps.PayMoney)                            
                            //where c.AllMoney!= payedmoney
                            orderby c.ContractDate descending
                            select new { ArmNames = GetArmariumName(c.ID), ArmOutPrice = GetArmOutPrice(c.ID), c.C1, c.ID,
                               c.ContractDate,c.ContactNum, cus.U_CName, c.AllMoney, chs.HospitalName, qiankuan = GetQianKuan(Convert.ToDouble(c.AllMoney), c.ID) };

            var query = from c in contracts
                        group c by c.ContactNum into g
                        select new { contactNum = g.Key, Sum = g.Sum(x => x.qiankuan) };

            this.DataPager.RecordCount = contracts.Count();
            this.DataPager.RecordCount = query.Count();
            //.net的自定义分页
            PagedDataSource pds = new PagedDataSource();
            pds.AllowPaging = true;
            pds.PageSize = PAGESIZE;
            pds.CurrentPageIndex = DataPager.CurrentPageIndex - 1;
            pds.DataSource = contracts.ToList();
            pds.DataSource = query.ToList();
            GvContract.DataSource = pds;
            GvContract.DataBind();
        }

            


------解决方案--------------------
你现在要替换 contracts 啊?
跑偏了。。。

contracts 里那么多字段呢。。。你对欠款的合计,单独求就好了啊。
没必要应加到contracts集合里啊。
------解决方案--------------------
contracts.ToList().Sum(c => c.qiankuan) 看看