C# C语言中的Linq查询Groupby#

C# C语言中的Linq查询Groupby#,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我有个人表和储蓄交易表 个人餐桌应该是这样的 [ID] [AccNum] [Name] [AccType] [IsMainPerson] [IsDelected] [RegionId] 1, 001, David, JoinAcc, True, False, NY 2, 002, John, SoloAcc, True, False, NY 3, 001, Terry, JoinAcc, False, False, NY [ID], [AccNum] [Amount] [IsDelete

我有个人表和储蓄交易表

个人餐桌应该是这样的

[ID] [AccNum] [Name] [AccType] [IsMainPerson] [IsDelected] [RegionId]

1, 001, David, JoinAcc, True, False, NY

2, 002, John, SoloAcc, True, False, NY

3, 001, Terry, JoinAcc, False, False, NY
[ID], [AccNum] [Amount] [IsDeleted]

1, 001, 10000, False

2, 002, 10000, False

3, 001, 15000, False
保存事务表时,可以这样做

[ID] [AccNum] [Name] [AccType] [IsMainPerson] [IsDelected] [RegionId]

1, 001, David, JoinAcc, True, False, NY

2, 002, John, SoloAcc, True, False, NY

3, 001, Terry, JoinAcc, False, False, NY
[ID], [AccNum] [Amount] [IsDeleted]

1, 001, 10000, False

2, 002, 10000, False

3, 001, 15000, False
我有一个用于保存人员列表和totalcount的viewmodel。我的totalcount视图是正确的,我的person list视图有问题

另一个列表中有一个RegionId。当我单击该列表时,我将RegionId作为一个参数,我想显示我的结果。所以,我用RegionId做了一个条件linq,并用相等的“AccNum”将两个表连接起来。如何修复linq查询以获得我想要的结果,如下图所示

我的控制器

[HttpGet]
        public ActionResult GetSavingGroupByRegion(string id = null)
        {
            IEnumerable<SavingGroupByRegionViewModel> savingbyRegion = (from p in db.TB_PersonalInformation.Where(x => x.RegionID.Equals(id) && x.IsDeleted != true && x.IsMainPerson == true).AsQueryable()
                                                                        join ADT in db.TB_AccountDetailTransaction on
                                                                        p.AccountNumber equals ADT.BankAccountNumber
                                                                        select new SavingGroupByRegionViewModel()
                                                                        {
                                                                            Name = p.Name,
                                                                            AccountNumber = p.AccountNumber,
                                                                            AccountType = p.AccountType,
                                                                            Address = p.Address,
                                                                            PersonAmount = ADT.Amount,
                                                                        }).ToList();
            SavingGroupByRegionViewModelWithCount savingbyRegionwithcount = new SavingGroupByRegionViewModelWithCount();
            savingbyRegionwithcount.RgnResult = savingbyRegion.ToList();
            savingbyRegionwithcount.TotalAmount = savingbyRegion.Select(t => Convert.ToDecimal(t.PersonAmount)).AsEnumerable().Sum();
            savingbyRegionwithcount.TotalTownship = db.TB_PersonalInformation.Where(t => t.RegionID.Equals(id)).Select(t => t.RegionID).Distinct().Count();
            savingbyRegionwithcount.TotalRegion = db.TB_PersonalInformation.Where(t => t.RegionID.Equals(id)).Select(t => t.RegionID).Distinct().Count();
            savingbyRegionwithcount.TotalPerson = (from a in db.TB_AccountDetailTransaction join p in db.TB_PersonalInformation.Where(x => x.RegionID.Equals(id) && x.IsDeleted != true && x.IsMainPerson == true) on a.BankAccountNumber equals p.AccountNumber select a).Where(x => x.IsDeleted != true).Select(x => x.BankAccountNumber).Distinct().Count();
            return PartialView("_SavingGroupByRegionPartial", savingbyRegionwithcount);
        }
--保存我得到的人员列表视图--

--保存要显示的人员列表视图--


我只想显示主要人员帐户名和帐户号码以及金额。我认为我的linq查询有问题。我试过了。distinct()但没有解决问题。我不知道如何分组。我希望有人能帮助我。谢谢

标题和变量名并不完全匹配,只需执行以下操作

savingbyRegion.GroupBy(x => new { Name = x.Name, No = x.AccountNumber, AccType=x.AccountType }).Select(x => new /*the model you're using*/ {No =x.Key.No,Name=x.Key.Name, PersonAmount=x.Sum(y=>y.Amount) /*Etc*/}); 
我应该给你

{No} {Name} {AccNum} {AccType} {Amount}

1, David, 001, JoinAcc, 25000

2, Join, 002, SoloAcc, 10000

为什么约翰有15000英镑?难道不是一万吗?@schlonzo我很抱歉我的坏。。。我会编辑它
savingbyRegion.GroupBy(x => new { Name = x.Name, No = x.AccountNumber, AccType=x.AccountType }).Select(x => new /*the model you're using*/ {No =x.Key.No,Name=x.Key.Name, PersonAmount=x.Sum(y=>y.Amount) /*Etc*/}); 
{No} {Name} {AccNum} {AccType} {Amount}

1, David, 001, JoinAcc, 25000

2, Join, 002, SoloAcc, 10000