Click here to Skip to main content
15,887,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm implementing asp.net core project. And I have multiple tables called ApiApp, ApiAppHistory and Entity. In ApiApp table I have date field which is of type string (yyyy/mm/dd) and a field ApplicantID. In ApiAppHistory there is a field called LastReqStatus which is of type int and its related value is stored in Entity table. Now I want to group by Year, Month, LastReqStatus and ApplicantID to find for each month of a year for how many ApplicantID the LastReqStatus is equal to "Granted". And after finding that I want to know the average for each month of a year that all the ApplicantIDs' has lastReqStatus == "Granted". For the former I wrote a query like the following:

But about the finding average I don't have any opinion how can I do that. I appreciate if anyone help me.
For Instance, I have the following data in ApiApp table :
ApiApp data table:

ID:1 LastRequestStatus:1 ApplicantID:1 ApiRequestDate:2019/02/03
ID:2 LastRequestStatus:2 ApplicantID:1 ApiRequestDate:2019/02/16

ID:3 LastRequestStatus:10 ApplicantID:2 ApiRequestDate:2019/02/01
ID:4 LastRequestStatus:19 ApplicantID:2 ApiRequestDate:2019/02/20
ID:5 LastRequestStatus:31 ApplicantID:2 ApiRequestDate:2019/02/29


Thus the average should be:

(2 + 3)/2 it means 2 is for ID:1 and ID:2 which has same Applicant #1 who requested in month Feb and 3 is for ID:3 and ID:4 and ID:5 which has same Applicant #2 who requested in month Feb and because in month Feb there are just two applicant #1 and #2 thus we have to divide the numerator to 2.


And my data models:

public partial class ApiApplicantHistory
    {
        public int Id { get; set; }
        public int? SentType { get; set; }
        public int? Reason { get; set; }
        public int? LastReqStatus { get; set; }
        public int? ApiAppId { get; set; }

        public virtual Apiapp ApiApp { get; set; }
        public virtual EntityType LastReqStatusNavigation { get; set; }
        public virtual EntityType SentTypeNavigation { get; set; }
        public virtual EntityType ReasonNavigation { get; set; }
    }

 public partial class EntityType
    {
        public EntityType()
        {
ApiApplicantHistoryLastReqStatusNavigation = new HashSet<ApiApplicantHistory>();            ApiApplicantHistorySentTypeNavigation = new HashSet<ApiApplicantHistory>();
            ApiApplicantHistoryReasonNavigation = new HashSet<ApiApplicantHistory>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public string EntityKey { get; set; }

public virtual ICollection<ApiApplicantHistory> ApiApplicantHistoryLastReqStatusNavigation { get; set; }        public virtual ICollection<ApiApplicantHistory> ApiAppHistorySentTypeNavigation { get; set; }
        public virtual ICollection<ApiApplicantHistory> ApiAppHistoryReasonNavigation { get; set; }

    }
}

public partial class Apiapp
    {
        public Apiapp()
        {
            ApiApplicantHistory = new HashSet<ApiApplicantHistory>();
        }

        public int Id { get; set; }
public bool? IsDeleted { get; set; }

        public int? LastRequestStatus { get; set; }
        public int ApplicantID { get; set; }
        public string ApiRequestDate { get; set; }
        public virtual ICollection<ApiApplicantHistory> ApiApplicantHistory { get; set; }
    }


What I have tried:

var avg = from t1 in _context.Apiapp
          join t2 in _context.ApiAppHistory on t1.Id equals t2.ApiAppId
          join t3 in _context.Entity on t2.LastReqStatus equals t3.Id
          where t1.IsDeleted == false && t1.LastRequestStatus == t2.Id && t3.Name == "Granted"
                group new {t1,t2}
                by new { t2.LastReqStatus, Year = t1.ApiRequestDate.Substring(0, 4), Month = t1.ApiRequestDate.Substring(5, 2) , Applicant = t1.ApplicantId } into g

                    select new
                             {

                                 Year = g.Key.Year, 
                                 Month = g.Key.Month,
                                 GrantedCount = g.Count()
                             };

var GrantedReqAVG = avg.ToList();
Posted
Updated 5-May-20 0:49am
v4

1 solution

Quote:
In ApiApp table I have date field which is of type string

This is bad idea, you have to convert it into datetime ASAP!

The only way to convert string data into proper datetime is (this should work also with EF):
1. DateTime.Parse()
or
2. DateTime.ParseExact()

So...
C#
//get proper values
var data = (from t1 in _context.Apiapp
            join t2 in _context.ApiAppHistory on t1.Id equals t2.ApiApplicantId
            join t3 in _context.Entity on t2.LastReqStatus equals t3.Id
            where t1.IsDeleted == false && t1.LastRequestStatus == t2.Id && t3.Name == "Granted" && t1.ApiRequestDate != null
            select new {ProperDate = DateTime.Parse(t1.ApiRequestDate), Applicant = t1.ApplicantId, ValueToGetAverage = ...})
            .ToList();

var avgdata = data
    .GroupBy(x=> new {FirstDayInMonth = x.ProperDate.AddDays(-x.ProperDate.Day+1), Applicant = Applicant})
    .Select(grp=> new
           {
               DateRange = g.Key.FirstDayInMonth,
               Applicatn = g.Key.Applicant,
               Average = grp.Average(x => x.ValueToGetAverage)
           }).ToList();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900