Home > Software engineering >  Cannot implicitly convert type to System.Collections.Generic.List to System.DateTime
Cannot implicitly convert type to System.Collections.Generic.List to System.DateTime

Time:01-18

I need to add 2 columns(LastTaskCreatedDate & LastTaskUpdatedDate) for a table. I have the query for that 2coulums and I just need to call that query into my repo function. My repo function already has a query inside it which get the other columns inside it. As i cant add another query inside, I creayed a function in it and added that query inside it. Please look into the Screenshots for better clarity.

enter image description herepic- The query which gets those two columns inside.

This is the repo funtion which gets the table data inside that and here i need to add those 2 columns also

        public WebStationResponse Filter(ProjectsFilter objFilter)
        {
            try
            {
                // Taking common project ids
                var projectIds = _currentUser.AccessInfo.UserProjects.Select(up => up.ProjectID).ToList();
                if (objFilter.ProjectID == null || objFilter.ProjectID.Count == 0)
                {
                    objFilter.ProjectID = projectIds;
                }
                objFilter.ProjectID = objFilter.ProjectID.Intersect(projectIds).ToList();
                objFilter.SetSqlPagingValues();
                objFilter.CompanyID = _currentUser.AccessInfo.CompanyID;
                objFilter.UserID = _currentUser.AccessInfo.UserID;
                objFilter.ServerTimezone = DefaultRepository.ServerTimezoneOffset();
                objFilter.UserTimezone = _currentUser.TimeZoneDetails.BaseUTCOffset;

                string strQuery = Project.GetProjects(objFilter);
                IList<ProjectDetails> lstProjectDetails = _dbContext.Database.Query<ProjectDetails>(strQuery,objFilter).ToList();
                if (lstProjectDetails.Count == 0)
                {
                    base.ResponseObject.ResponseId = (int)ResponseCode.RecordDoesnotExist;
                    return base.ResponseObject;
                }
                //
                int intRecordCount = lstProjectDetails.Count;
                if(objFilter.Pagination)
                {
                    intRecordCount = _dbContext.Database.ExecuteScalar<int>(Common.RecordCount());
                }
                FillPermissions(ref lstProjectDetails);
                FillProjectCompetencies(ref lstProjectDetails);
                **FillLastDates(ref lstProjectDetails); **
                base.FillResponseDetails(null,lstProjectDetails,null);
                base.ResponseObject.RecordCount = intRecordCount;
            }
            catch (Exception ex)
            {
                base.FillResponseDetails(ex, null, null);
            }
            return base.ResponseObject;
        }`your text`

This is the function I added and inside it has the query- GetLastTaskUpdateDates.

        private void FillLastDates(ref IList<ProjectDetails> lstProjects)
        {
            if (lstProjects == null || lstProjects.Count == 0)
            {
                return;
            }
            //
            var projectIds = lstProjects.Select(p => p.ProjectID).ToList();
            string strQuery = Project.GetLastTaskUpdatedDates();
            IList<ProjectDetails> lstDates = _dbContext.Database.Query<ProjectDetails>(strQuery, new { ProjectID = projectIds }).ToList();
            //
            ProjectDetails objProject = null;
            for (int intIndex = 0; intIndex < lstProjects.Count; intIndex  )
            {
                objProject = lstProjects[intIndex];
                lstProjects[intIndex].LastTaskCreated = lstDates.Where(c => c.ProjectID == objProject.ProjectID).ToList();
                lstProjects[intIndex].LastTaskUpdated= lstDates.Where(c =>c.ProjectID == objProject.ProjectID).ToList();

            }
        }`your text`

If we look at last two lines, I am getting this error- Cannot implicitly convert type to System.Collections.Generic.List to System.DateTime

Need help!!, Please reply for any other clarity on the problem

Gave everything in the details of the problem

CodePudding user response:

LastTaskCreate and LastTaskUpdated are DateTime and you try to put lists instead of DateTimes, it's normal.

You must get only one date from the list you have. I think what you're looking for is something like that (assuming that lstDates is a list of dates):

var projectDates = lstDates.Where(c => c.ProjectID == objProject.ProjectID).OrderBy(e => e); // Ordered IEnumerable (it's a list but a bit different) of dates from the oldest to the newest
lstProjects[intIndex].LastTaskCreated = projectDates.First(); // Gives the first elem of the enumerable, should be the date of creation
lstProjects[intIndex].LastTaskUpdated = projectDates.Last(); // Gives the last elem of the enumerable, should be the last update date

I didn't put the null checkers but you might want to add some to be sure that projectDates is not empty.

If lstDates is a list of projects, the code could be this:

var project = lstDates.FirstOrDefault(c => c.ProjectID == objProject.ProjectID);

if (project != null)
{
    lstProjects[intIndex].LastTaskCreated = project.LastTaskCreated 
    lstProjects[intIndex].LastTaskUpdated = project.LastTaskUpdated 
}

but it depends on what's inside ProjectDetails.

Hope it helped

CodePudding user response:

Although I don't fully understand what's going on in your scenario, the error seems to be fairly explainatory?

lstProjects[intIndex].LastTaskCreated 

expects a Datetime, whereas

lstDates.Where(c => c.ProjectID == objProject.ProjectID).ToList();

is a list of ProjectDetails.

I suspect you want something like:

lstProjects[intIndex].LastTaskCreated = lstDates.First(c => c.ProjectID == objProject.ProjectID).LastTaskCreated;
  • Related