繁体   English   中英

Linq 查询 C# 中的 Groupby

[英]Linq query Groupby in C#

我有个人表和保存交易表。

个人表是这样的。

[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

我有一个用于保存人员列表和总数的视图模型。 我的总数视图是正确的,但我的人员列表视图有问题。

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

我的 Controller

[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);
        }

我的视图模型

public class SavingGroupByRegionViewModelWithCount
    {
        public decimal TotalAmount { get; set; }
        public int TotalTownship { get; set; }
        public int TotalRegion { get; set; }
        public int TotalPerson { get; set; }
        public List<SavingGroupByRegionViewModel> RgnResult { get; set; }
    }
    public class SavingGroupByRegionViewModel
    {
        public string Name { get; set; }
        public string AccountNumber { get; set; }
        public string AccountType { get; set; }
        public string Address { get; set; }
        public string PersonAmount { get; set; }
    }

我想要这样的视图结果

我的查看结果

--计数视图--

{Total Amount} {Total Person Count}

35000 , 2

--保存我得到的人员列表视图--

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

1, David, 001, JoinAcc, 10000

2, David, 001, JoinAcc, 15000

3, John, 002, SoloAcc, 10000

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

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

1, David, 001, JoinAcc, 25000

2, Join, 002, SoloAcc, 10000

我只想显示主要个人帐户名称和带有总金额的帐号。 我认为我的 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM