取最后一个 VchNo 和按月分组并在实体框架中求和总金额

Posted

技术标签:

【中文标题】取最后一个 VchNo 和按月分组并在实体框架中求和总金额【英文标题】:Take Last VchNo and Group by Month and Sum Total Amount in Entity Framework 【发布时间】:2021-07-20 19:27:59 【问题描述】:
public partial class FeesVoucher
    
        public long Auto_id  get; set; 
        public string VoucherNo  get; set; 
        public string StudentID  get; set; 
        public string SessionID  get; set; 
        public string ClassID  get; set; 
        public string SectionID  get; set; 
        public Nullable<System.DateTime> IssueDate  get; set; 
        public Nullable<System.DateTime> DueDate  get; set; 
        public Nullable<System.DateTime> FeesDate  get; set; 
        public string FeeMonth  get; set; 
        public string FeeYear  get; set; 
        public Nullable<decimal> RegistrationFee  get; set; 
        public Nullable<decimal> AdmissionFee  get; set; 
        public Nullable<decimal> SecurityDeposit  get; set; 
        public Nullable<decimal> TutionFee  get; set; 
        public Nullable<decimal> Fine_Late_Fee  get; set; 
        public Nullable<decimal> TransportCharges  get; set; 
        public Nullable<decimal> Lab_ExaminationFee  get; set; 
        public Nullable<decimal> AnnualCharges  get; set; 
        public Nullable<decimal> BoardPECFee  get; set; 
        public Nullable<decimal> OtherCharges  get; set; 
        public Nullable<decimal> TotalAmount  get; set; 
        public string FeesStatus  get; set; 
        public string PaidType  get; set; 
        public string CampusID  get; set; 
        public string FeesID  get; set; 
        public string VoucherMessages  get; set; 
        public string CreatedBy  get; set; 
        public Nullable<System.DateTime> CreatedDate  get; set; 
        public string LastUpdateBy  get; set; 
        public Nullable<System.DateTime> LastUpdated  get; set; 
        public string EditMsg  get; set; 
        public string IsEdit  get; set; 
        public string RevAcc  get; set; 
        public string RecAcc  get; set; 
    

using (Entities db = new Entities())
                    

                        var dt = (from stu in db.StudentsInfoes
                                  join CampusS in db.CampusSetups on stu.CampusID equals CampusS.CampusID
                                  join stuclass in db.StudentClassInfoes on stu.StudentID equals stuclass.StudentID
                                  join sectionID in db.ClassesSections on stuclass.ClassID equals sectionID.ClassID
                                  join classs in db.ClassSetups on stuclass.ClassID equals classs.ClassID
                                  join SessS in db.SessionsSetups on stuclass.SessionID equals SessS.SessionID
                                  join FeeV in db.FeesVouchers on stu.StudentID equals FeeV.StudentID 
                                  
                                  

                                  select new
                                  

                                      StudentID = stu.StudentID,
                                      FullName = stu.FullName,
                                      FatherName = stu.FatherName,
                                      StStatus = stu.Status,
                                      ClassStatus = stuclass.Status,
                                      CampusID = CampusS.CampusID,
                                      RollNo = stuclass.RollNo,
                                      ClassID = stuclass.ClassID,
                                      ClassDesc = classs.ClassDesc,
                                      SectionID = sectionID.SectionID,
                                      SectionDesc = sectionID.SectionDesc,
                                      VoucherNo = FeeV.VoucherNo,
                                      SessionID = SessS.SessionID,
                                      Months = FeeV.FeeMonth,
                                      RegistrationFee = FeeV.RegistrationFee,
                                      AdmissionFee = FeeV.AdmissionFee,
                                      AnnualCharges = FeeV.AnnualCharges,
                                      SecurityDeposit = FeeV.SecurityDeposit,
                                      Fine_Late_Fee = FeeV.Fine_Late_Fee,
                                      BoardPECFee = FeeV.BoardPECFee,
                                      Lab_ExaminationFee = 
                                      FeeV.Lab_ExaminationFee,
                                      OtherCharges = FeeV.OtherCharges,
                                      TutionFee = FeeV.TutionFee,
                                      IssueDate = FeeV.IssueDate,
                                      DueDate = FeeV.DueDate,
                                      
                                      TotalAmount = FeeV.TotalAmount,
                                      FeeStatus = FeeV.FeesStatus,
                                      FeeYear = FeeV.FeeYear,
                                      FeesID = FeeV.FeesID,

                                  

                              ).Where(x => x.FeeStatus.Equals("unpaid")).Select(x => new x.VoucherNo,x.FullName,x.IssueDate,x.DueDate,x.Months,x.TotalAmount ).Distinct().ToList();

这个查询在没有分组和求和的情况下完美地工作,如图所示 ===> Without Group and Sum Picture 在多个表加入此查询后生成这样的结果

VchNo Student Name Month Total Amount
0001 aa Jan 2000
0002 aa Feb 2000
0003 aa Mar 2000
0004 bb Jan 2000
0005 bb Feb 2000
0006 bb Mar 2000

但我想修改我的查询以实现分组和求和,如图所示 ===>With Group and Sum Picture 但我想取最后一个 VchNo 并按月分组并像这样总结总金额

VchNo Student Name Month Total Amount
0003 aa Jan,Feb,Mar 6000
0006 bb Jan,Feb,Mar 6000

实体框架 6.0 的查询是什么 所有答案将不胜感激

【问题讨论】:

【参考方案1】:

见下文:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication

    public class Program
    
        
        public static void Main(string[] args)
        
            DataTable dt = new DataTable();
            dt.Columns.Add("VchNo", typeof(string));
            dt.Columns.Add("Student Name", typeof(string));
            dt.Columns.Add("Month", typeof(string));
            dt.Columns.Add("Total Amount", typeof(int));

            dt.Rows.Add(new object[]  "0001", "aa", "Jan", 2000 );
            dt.Rows.Add(new object[]  "0002", "aa", "Feb", 2000 );
            dt.Rows.Add(new object[]  "0003", "aa", "Mar", 2000 );
            dt.Rows.Add(new object[]  "0004", "bb", "Jan", 2000 );
            dt.Rows.Add(new object[]  "0005", "bb", "Feb", 2000 );
            dt.Rows.Add(new object[]  "0006", "bb", "Mar", 2000 );

            DataTable dt2 = dt.Clone();

            var groups = dt.AsEnumerable().GroupBy(x => x.Field<string>("Student Name")).ToList();

            foreach (var group in groups)
            
                dt2.Rows.Add(new object[] 
                    group.Select(x => x.Field<string>("VchNo")).LastOrDefault(),
                    group.Key,
                    string.Join(",", group.Select(XmlReadMode => XmlReadMode.Field<string>("Month"))),
                    group.Sum(x => x.Field<int>("Total Amount"))
                );
            

        
    



使用实体:

    class Program
    
        static void Main(string[] args)
        

            using (Entities db = new Entities())
            

                var dt = (from stu in db.StudentsInfoes
                          join CampusS in db.CampusSetups on stu.CampusID equals CampusS.CampusID
                          join stuclass in db.StudentClassInfoes on stu.StudentID equals stuclass.StudentID
                          join sectionID in db.ClassesSections on stuclass.ClassID equals sectionID.ClassID
                          join classs in db.ClassSetups on stuclass.ClassID equals classs.ClassID
                          join SessS in db.SessionsSetups on stuclass.SessionID equals SessS.SessionID
                          join FeeV in db.FeesVouchers on stu.StudentID equals FeeV.StudentID

                          select new
                          

                              StudentID = stu.StudentID,
                              FullName = stu.FullName,
                              FatherName = stu.FatherName,
                              StStatus = stu.Status,
                              ClassStatus = stuclass.Status,
                              CampusID = CampusS.CampusID,
                              RollNo = stuclass.RollNo,
                              ClassID = stuclass.ClassID,
                              ClassDesc = classs.ClassDesc,
                              SectionID = sectionID.SectionID,
                              SectionDesc = sectionID.SectionDesc,
                              VoucherNo = FeeV.VoucherNo,
                              SessionID = SessS.SessionID,
                              Months = FeeV.FeeMonth,
                              RegistrationFee = FeeV.RegistrationFee,
                              AdmissionFee = FeeV.AdmissionFee,
                              AnnualCharges = FeeV.AnnualCharges,
                              SecurityDeposit = FeeV.SecurityDeposit,
                              Fine_Late_Fee = FeeV.Fine_Late_Fee,
                              BoardPECFee = FeeV.BoardPECFee,
                              Lab_ExaminationFee =
                              FeeV.Lab_ExaminationFee,
                              OtherCharges = FeeV.OtherCharges,
                              TutionFee = FeeV.TutionFee,
                              IssueDate = FeeV.IssueDate,
                              DueDate = FeeV.DueDate,

                              TotalAmount = FeeV.TotalAmount,
                              FeeStatus = FeeV.FeesStatus,
                              FeeYear = FeeV.FeeYear,
                              FeesID = FeeV.FeesID,

                          

                      ).Where(x => x.FeeStatus.Equals("unpaid")).Select(x => new  x.VoucherNo, x.FullName, x.IssueDate, x.DueDate, x.Months, x.TotalAmount ).Distinct().ToList();

                var results = dt.GroupBy(x => x.FullName).Select(x => new
                
                    VchNo = x.Last().VoucherNo,
                    Name = x.Key,
                    Months = string.Join(",", x.Select(y => y.Months)),
                    Amount = x.Sum(y => y.TotalAmount)

                ).ToList();
            
        
    
    public class Entities : IDisposable
    
        public List<CampusSetups> CampusSetups  get; set; 
        public List<ClassSetups> ClassSetups  get; set; 
        public List<ClassesSections> ClassesSections  get; set; 
        public List<FeesVoucher> FeesVouchers  get; set; 
        public string SectionsSetups  get; set; 
        public List<StudentsInfoes> StudentsInfoes  get; set; 
        public List<StudentClassInfoes> StudentClassInfoes  get; set; 
        public List<SessionsSetups> SessionsSetups  get; set; 

        public void Dispose()
        
        
    
    public class ClassSetups
    
        public string ClassID  get; set; 
        public string ClassDesc  get; set; 
    
    public class ClassesSections
    
        public string ClassID  get; set; 
        public string SectionDesc  get; set; 
        public string SectionID  get; set; 
    
    public class SessionsSetups
    
        public string SessionID  get; set; 
    
    public class StudentClassInfoes
    
        public string StudentID  get; set; 
        public string ClassID  get; set; 
        public string SessionID  get; set; 
        public string RollNo  get; set; 
        public string Status  get; set; 
    
    public class CampusSetups
    
        public string CampusID  get; set; 
    
    public class StudentsInfoes
    
        public string CampusID  get; set; 
        public string StudentID  get; set; 
        public string FatherName  get; set; 
        public string FullName  get; set; 
        public string Status  get; set; 
    
    public partial class FeesVoucher
    
        public long Auto_id  get; set; 
        public string VoucherNo  get; set; 
        public string StudentID  get; set; 
        public string SessionID  get; set; 
        public string ClassID  get; set; 
        public string SectionID  get; set; 
        public Nullable<System.DateTime> IssueDate  get; set; 
        public Nullable<System.DateTime> DueDate  get; set; 
        public Nullable<System.DateTime> FeesDate  get; set; 
        public string FeeMonth  get; set; 
        public string FeeYear  get; set; 
        public Nullable<decimal> RegistrationFee  get; set; 
        public Nullable<decimal> AdmissionFee  get; set; 
        public Nullable<decimal> SecurityDeposit  get; set; 
        public Nullable<decimal> TutionFee  get; set; 
        public Nullable<decimal> Fine_Late_Fee  get; set; 
        public Nullable<decimal> TransportCharges  get; set; 
        public Nullable<decimal> Lab_ExaminationFee  get; set; 
        public Nullable<decimal> AnnualCharges  get; set; 
        public Nullable<decimal> BoardPECFee  get; set; 
        public Nullable<decimal> OtherCharges  get; set; 
        public Nullable<decimal> TotalAmount  get; set; 
        public string FeesStatus  get; set; 
        public string PaidType  get; set; 
        public string CampusID  get; set; 
        public string FeesID  get; set; 
        public string VoucherMessages  get; set; 
        public string CreatedBy  get; set; 
        public Nullable<System.DateTime> CreatedDate  get; set; 
        public string LastUpdateBy  get; set; 
        public Nullable<System.DateTime> LastUpdated  get; set; 
        public string EditMsg  get; set; 
        public string IsEdit  get; set; 
        public string RevAcc  get; set; 
        public string RecAcc  get; set; 
    

【讨论】:

非常感谢自定义数据表。我想要关于 LINQ 或实体框架上下文的任何建议! Linq 是一个查询,并不意味着将数据添加到数据表中。代码很容易转换为实体。 谢谢,兄弟。问题已更新。现在您可以为我提出更好的解决方案吗? Distinct 真的有效吗?您需要以下内容才能工作:docs.microsoft.com/en-us/dotnet/api/… 添加实体解决方案【参考方案2】:

也许是这样的

collection.GroupBy(x => x.StudentName).Select(g =>
  new
    g.Last().VchNo,
    g.Last().StudentName,
    Month = string.Join(',', g.Select(gg => g.Month)),
    TotalAmount = g.Sum(gg => g.TotalAmount)
  
);

我猜到了属性名称,因为您没有包含它们。修改代码以替换您的实际实体名称

【讨论】:

g.Month 和 g.TotalAmount 显示错误“不包含 Month 和 TotalAmount 的定义 您没有指定您的客户端属性名称是什么(但它们肯定不能包含空格!)您只是说数据库表的样子(您的列名真的包含空格??) .您必须将它们更改为您的实际属性名称。如果您没有在问题中提供完整的信息,您通常必须接受答案可能无法直接复制粘贴;仍然需要你的努力 非常感谢。我已经用代码和两张图片更新了我的问题。现在你可以建议我更好的查询。

以上是关于取最后一个 VchNo 和按月分组并在实体框架中求和总金额的主要内容,如果未能解决你的问题,请参考以下文章

NSFetchRequest 按月分组

sql按月统计数量和按月累加统计数量

按年和按月创建表空间

sql 按月和按年收到的产品订单数量

按月/年分组 FechedResults 以显示带有部分的 swiftui 列表

如何在 BigQuery 中按外键分组?