0

I have this piece of code that works perfectly on LinqPad. But when I implement it in Visual Studio it returns no data. Does anyone knows what is wrong? The error I get is Object reference not set to an instance of an object.

from site in Sites
orderby site.Community.Name ascending
where site.Yard.YardID == 1 && site.SiteTypeID == 1
let Cycles = (from sbm in SBMs
              where sbm.CrewSite.SiteID == site.SiteID && sbm.CrewSite.Crew.CrewDate.Year == 2019
              orderby sbm.CrewSite.Crew.CrewDate ascending
              select new
              {
                  Date = sbm.CrewSite.Crew.CrewDate

              }).ToList()
select new 
{
    Pin = site.Pin,
    Community = site.Community.Name,
    Neighbourhood = site.Neighbourhood,
    Address = site.StreetAddress,
    Area = site.Area,
    Notes = site.Notes,
    Cycle1 = Cycles.OrderBy(x => x.Date).FirstOrDefault().Date.Equals(null) ? (DateTime?)null : Cycles.OrderBy(x => x.Date).FirstOrDefault().Date,
    Cycle2 = Cycles.OrderBy(x => x.Date).Skip(1).FirstOrDefault().Equals(null) ? (DateTime?)null : Cycles.OrderBy(x => x.Date).Skip(1).FirstOrDefault().Date,
    Cycle3 = Cycles.OrderBy(x => x.Date).Skip(2).FirstOrDefault().Equals(null) ? (DateTime?)null : Cycles.OrderBy(x => x.Date).Skip(2).FirstOrDefault().Date,
    Cycle4 = Cycles.OrderBy(x => x.Date).Skip(3).FirstOrDefault().Equals(null) ? (DateTime?)null : Cycles.OrderBy(x => x.Date).Skip(3).FirstOrDefault().Date,
    Cycle5 = Cycles.OrderBy(x => x.Date).Skip(4).FirstOrDefault().Equals(null) ? (DateTime?)null : Cycles.OrderBy(x => x.Date).Skip(4).FirstOrDefault().Date
}
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • What is `Sites` in Linqpad and what is it in Visual Studio? Look very closely at the differences in types and try to find out why they behave differently. If that doesn't put you on the right track (one hint: Linqpad uses LINQ-to-SQL by default) then add more details to your question about these types and versions of used components (like Entity Framework, presumably). – Gert Arnold Mar 23 '20 at 21:31
  • Please see bellow the code I use in Visual Studio. I read the info found in the link posted above. However I trued to put break points in this method but I couldn't see the values that were pulled out of the database. at the end I still had the same error. – J. Raoul FEZE Mar 23 '20 at 22:18
  • well it looks like I can't post that code because it is too long. – J. Raoul FEZE Mar 23 '20 at 22:30

0 Answers0