none
Get total count of days between two dates Linq C# RRS feed

  • Question

  • I want to get total number of days between two dates using Linq C# with Entity Framework 6. I am getting error when the code runs. Below is my code. Any help to achieve this will be appreciated.

       var finalexams = report.Select(x =>		            
    		         new ReportVieModel
    		        {
    		         
    		           EnglishDates = x.admin.mastertablebles.Where(d => d.EndDate == null).Select(s => DbFunctions.DiffDays(s.beginDate, DateTime.Today)),
                     
                        
                    }).Distinct();


    Ebenezer

    Saturday, November 23, 2019 3:29 AM

Answers

  • I am getting error when the code runs.

    And what error would that be? Linq is not a panacea solution. You probably cannot get this to work if Linq-2-Entities is involved since that would involve the EF engine to create T-SQL that it cannot do due to the construction of the Linq query.

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ef/language-reference/linq-to-entities

    IMO, you could get this to work if calling a stored procedure using the EF backdoor in EF 6.

    Or you pull all of the objects based on not using the date creitera, close the connection with EF and the database that makes the queried results a disconnected result and also makes the collection quarriable by Linq-2-Objects

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/linq-to-objects

    And in using the below in a Linq projection you could get the result you are looking for.

    https://www.w3resource.com/csharp-exercises/datetime/csharp-datetime-exercise-30.php

    You could probably get it to work using Linq-2-Entities if using a Linq projection using anonymous types.

     select new
                    {
                       
                        NumberOfDays = CalaculateNoOfDays(BeginDate)
                    };

    CalaculateNoOfDays is function that can be called in the Linq projection.

    • Marked as answer by denkyira Sunday, November 24, 2019 4:50 AM
    Saturday, November 23, 2019 4:45 AM

All replies

  • I am getting error when the code runs.

    And what error would that be? Linq is not a panacea solution. You probably cannot get this to work if Linq-2-Entities is involved since that would involve the EF engine to create T-SQL that it cannot do due to the construction of the Linq query.

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ef/language-reference/linq-to-entities

    IMO, you could get this to work if calling a stored procedure using the EF backdoor in EF 6.

    Or you pull all of the objects based on not using the date creitera, close the connection with EF and the database that makes the queried results a disconnected result and also makes the collection quarriable by Linq-2-Objects

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/linq-to-objects

    And in using the below in a Linq projection you could get the result you are looking for.

    https://www.w3resource.com/csharp-exercises/datetime/csharp-datetime-exercise-30.php

    You could probably get it to work using Linq-2-Entities if using a Linq projection using anonymous types.

     select new
                    {
                       
                        NumberOfDays = CalaculateNoOfDays(BeginDate)
                    };

    CalaculateNoOfDays is function that can be called in the Linq projection.

    • Marked as answer by denkyira Sunday, November 24, 2019 4:50 AM
    Saturday, November 23, 2019 4:45 AM
  • Hi 

    Thanks. You are absolutely correct. I could get it working with Linq-2-Entities then project the results to the viewModel.


    Ebenezer

    Sunday, November 24, 2019 4:49 AM