I have Personal Table and Saving Transaction Table.
Personal Table be like this.
[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
Saving Transaction Table be like this.
[ID], [AccNum] [Amount] [IsDeleted]
1, 001, 10000, False
2, 002, 10000, False
3, 001, 15000, False
I have a viewmodel for saving person list and totalcount. My totalcount view is correct and I have a problem with person list view.
There is a RegionId from another list. When I click that list, I took RegionId as a parameter and I want to show my result. So, I made a conditional linq with RegionId and joining of two table by equal "AccNum". How can I fix my linq query in order to get the result I want that I have shown in below.
My 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);
}
My Viewmodel
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; }
}
I want the view result like this
My View result
--Count View--
{Total Amount} {Total Person Count}
35000 , 2
--Saving Person List View I got--
{No} {Name} {AccNum} {AccType} {Amount}
1, David, 001, JoinAcc, 10000
2, David, 001, JoinAcc, 15000
3, John, 002, SoloAcc, 10000
--Saving Person List View I want to show--
{No} {Name} {AccNum} {AccType} {Amount}
1, David, 001, JoinAcc, 25000
2, Join, 002, SoloAcc, 10000
I want to show only main person account name and account number with sum amount. I think there is something wrong in my linq query. I tried.distinct() and it didn't solve. I don't know how to group by. I hope someone could help me. Thanks
The headers and the variable names don't really match, but simply doing something like
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*/});
should get you
{No} {Name} {AccNum} {AccType} {Amount}
1, David, 001, JoinAcc, 25000
2, Join, 002, SoloAcc, 10000
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.