Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm implementing asp.net core 3.1. I have written some code like the following and now I want to get the result of these two queries that has the same size and iterate over each one of them and divide their element and store the result in a list. But now the problem is in my zip method, I can not specify exactly which attribute of each query I want to divide.
Inside the body of foreach, r.Num and r.Denum is unknown! I appreciate of any help to fix the error.

What I have tried:

var mytotal = _context.Apiapp.GroupBy(o => new
        {
            Month = o.ApiRequestDate.Substring(4, 2),
            Year = o.ApiRequestDate.Substring(0, 4)
        }).Select(g => new
        {
            Month = g.Key.Month,
            Year = g.Key.Year,
            Total = g.Count()
        }).OrderByDescending(a => a.Year).ThenByDescending(a => a.Month).ToList();

        var numerator =  from t1 in _context.Apiapp
                         join t2 in _context.ApiAppHistory on t1.Id equals t2.ApiApplicantId
                         join t3 in _context.EntityType on t2.LastReqStatus equals t3.Id
                         where t1.IsDeleted == false && t1.LastRequestStatus == t2.Id && t3.Name == "granted"
                         group new { Year = t1.ApiRequestDate.Substring(0, 4), Month = t1.ApiRequestDate.Substring(4, 2) }
                         by new { t2.LastReqStatus } into g
                         select new
                         {
                             Year = g.Max(n => n.Year),
                             Month = g.Max(n => n.Month),
                             GrantedCount = g.Count()
                         };
        var GrantedReqStatus = numerator.ToList();
        var GrantedAccessPercent = new List<Double>();

        //-------------------------------------------------------
        var res = mytotal.Zip(GrantedReqStatus, (total, GrantedCount) => new { Num = total, Denum = GrantedCount });
        foreach(var r in res)
        {
            GrantedAccessPercent.Add(r.Num/r.Denum);
        }
Posted
Updated 7-May-20 8:28am

1 solution

The error isn't that r.Num and r.Denum are unknown; the error is that the division operator isn't defined for the anonymous types.

Change your code to:
C#
var res = mytotal.Zip(GrantedReqStatus, (n, d) => new { Num = n.Total, Denum = d.GrantedCount });
foreach (var r in res)
{
    GrantedAccessPercent.Add((double)r.Num / r.Denum);
}
Or, more simply:
C#
var GrantedAccessPercent = mytotal.Zip(GrantedReqStatus, (n, d) => (double)n.Total / d.GrantedCount }).ToList();
NB: You need to cast either the numerator or the denominator to double before dividing, otherwise the result will be truncated.
 
Share this answer
 

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