tags:

views:

95

answers:

1

I am having a problem with LINQ and I was hoping someone could explain to me why. I have this code:

        List<Spec> specs = GetSpecs(userObject, seasonID, partnershipID);

        var query = from s in specs
                    where (DateTime)s.FinalApprovedDate != null
                    && !((DateTime)s.FinalApprovedDate).Equals(DateTime.Parse("1/1/1900 12:00:00 AM"))
                    group s by s.ForCompanyID into g 
                    select new
                    {
                        Vendor = g.Key,
                        Avg = g.Average(s.FinalApprovedDate.Subtract((DateTime)s.Created_Date).Days)
                    };

What I would like to retrieve from this query is the average difference in days between the Created Date and the Final Approved Date for each Company represented byt the ForCompanyID where the dates are not null or '1/1/1900'. My issue is that I cannot figure out how to get this data from this query. From what I've read I believe that this is the correct syntax but I get an "The name 's' does not exist in the current context on the s values under new. I have also tried this with g here but that does not have the values either. Any thoughts?

+7  A: 

Try changing the penultimate line to:

Avg = g.Average(s => s.FinalApprovedDate.Subtract((DateTime)s.Created_Date).Days)
mquander
+1 for using penultimate correctly. :)
Randolpho
will probably need to cast s.FinalApprovedDate to DateTime (again) to use Subtract()
Lucas
Thanks for pointing me in the right direction. Here is what I got to work for me:select new { Vendor = g.Key, Avg = g.Average(s => ((DateTime)s.FinalApprovedDate - (DateTime)s.Created_Date).Days) };
bechbd