Click here to Skip to main content
15,911,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am getting not supported exception while calling methods to set values to anonymous type. I can understand why is it but is there any other way to achieve what I am trying to do. please look at the code below.

What I have tried:

private static double CalculateAvgDayFromDates(DateTime date, DateTime productionStartDate)
        {
            return (Convert.ToDateTime(date.ToShortDateString()) - Convert.ToDateTime(productionStartDate.ToShortDateString())).TotalDays + GetPercentageOfDay(date.TimeOfDay);
           
        }
        public static void GetDirectorDetails()
        {
            try
            {
                var sdt = new DateTime(2018, 2, 3);
                var edt = new DateTime(2019, 2, 1);

                using (var db = new MyDbContext())
                {
                    var ManagerDetails = from p in db.Projects
                                          let projHOurs = db.ProjectHours.Where(ph => ph.ProjectID == p.ID).Sum(h => h.DayHours)
                                          //let lDate = CalculateAvgDayFromDates(p.ReadyToLaunchDate.Value, p.ProductionStartDate.Value)
                                          //let sLDate = CalculateAvgDayFromDates(p.ProductionStartDate.Value.AddDays(6), p.ProductionStartDate.Value)
                                          where (p.ProductionStartDate > sdt && p.ProductionStartDate <= edt)
                                          where (p.ProductionCompleteDate != null && p.ProductType == 1 && p.Active)
                                         
                                          select new
                                          {
                                              Manager = p.Manager,
                                              SalesEmployee = p.SalesEmployee,
                                              StoreNumber = p.StoreNumber,
                                              LaunchDate = CalculateAvgDayFromDates(p.ReadyToLaunchDate.Value, p.ProductionStartDate.Value),
                                              SoftLaunchDate = CalculateAvgDayFromDates(p.ProductionStartDate.Value.AddDays(6), p.ProductionStartDate.Value),
                                              totalWorkedHours = projHOurs

                                          };

                    var Query = from d in directorDetails
                                  group d by d.director into g
                                  select new
                                  {
                                      manager = g.Key,
                                      storeCounts = g.Count(),
                                      avgWorkedHours = g.Average(x => x.totalWorkedHours),
                                      avgLaunchDays = g.Average(x => x.LaunchDate),
                                      avgSoftLaunchDays = g.Average(x => x.SoftLaunchDate)
                                  };
                   
                    foreach (var d in Query)
                    {
                        System.Diagnostics.Debug.WriteLine($"dir:{d.manager}, storeCounts: {d.storeCounts}, avgLaunchDays: {d.avgLaunchDays}, avgSoftLaunchDays:{d.avgSoftLaunchDays}, avgWorkedHours: {d.avgWorkedHours}");
                      
                    }
                }
            }
            catch (Exception ex)
            {

                string s = ex.Message;
                System.Diagnostics.Debug.WriteLine(s);
            }
        }
Posted
Updated 30-Aug-18 7:46am

1 solution

Entity Framework doesn't know how to convert your CalculateAvgDayFromDates method to a SQL query.

You'll need to pull the records into memory before performing that calculation.
C#
var managerQuery = from p in db.Projects
                   let projHOurs = db.ProjectHours.Where(ph => ph.ProjectID == p.ID).Sum(h => h.DayHours)
                   where (p.ProductionStartDate > sdt && p.ProductionStartDate <= edt)
                   where (p.ProductionCompleteDate != null && p.ProductType == 1 && p.Active)
                   select new
                   {
                       p.Manager,
                       p.SalesEmployee,
                       p.StoreNumber,
                       p.ReadyToLaunchDate,
                       p.ProductionStartDate,
                       projHOurs,
                   };

var ManagerDetails = from r in managerQuery.AsEnumerable()
                     select new
                     {
                         r.Manager,
                         r.SalesEmployee,
                         r.StoreNumber,
                         LaunchDate = CalculateAvgDayFromDates(r.ReadyToLaunchDate.Value, r.ProductionStartDate.Value),
                         SoftLaunchDate = CalculateAvgDayFromDates(r.ProductionStartDate.Value.AddDays(6), r.ProductionStartDate.Value),
                         totalWorkedHours = r.projHOurs,
                     };

The AsEnumerable() call forces the following LINQ methods to be executed in memory, rather than trying to convert them to SQL.
 
Share this answer
 
Comments
istudent 30-Aug-18 14:16pm    
Thank you sir

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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