none
Linq to Sql The argument 'value' was the wrong type - RRS feed

  • Question

  • I am having some very odd results. I have a manager class structured as follows:

     

    public static Collection<Employee> GetEmployees(EmployeeCriteria criteria)
        {
          using (DboDataContext ctx = ContextManager.DboContext)
          {
            return new Collection<Employee>(GetEmployeesQuery(ctx, criteria).ToList());
          }
        }
    
    private static IQueryable<Employee> GetEmployeesQuery(DboDataContext ctx, EmployeeCriteria criteria)
        {
          if (criteria.PayPeriod == null)
          {
            throw new ArgumentException("PayPeriod criteria cannot be null when doing an employee query.");
          }
    
          
          return ctx.SelectEmployeesDynamic(
            criteria.EmployeeId,
            criteria.OrganizationId,
            criteria.OrganizationalUnitId,
            criteria.DodPersonId,
            criteria.IsActive,
            criteria.IsAlternateLeaveApprover,
            criteria.IsTravelCompensationApprover,
            criteria.IsSupervisor,
            criteria.IsTravelApprover,
            criteria.IsTimekeeper,
            criteria.PayPeriod.StartDate,
            criteria.CoworkerEmployeeId,
            criteria.TimekeeperEmployeeId,
            (int?)criteria.CurrentWorkScheduleType,
            (int?)criteria.PermanentWorkScheduleType).Select(o => new Employee
            {
              Id = o.EmployeeId,
              CurrentWorkScheduleId = o.PayPeriodWorkScheduleId,
              CurrentWorkScheduleEffectivePayPeriod = o.PayPeriodWorkScheduleId.HasValue ? (o.PayPeriodPermanentWorkScheduleId.HasValue && o.PayPeriodWorkScheduleId == o.PayPeriodPermanentWorkScheduleId ? PayPeriod.GetPayPeriod(o.PayPeriodPermanentWorkScheduleEffectivePayPeriodStartDate.Value) : PayPeriod.GetPayPeriod(criteria.PayPeriod.StartDate)) : null,
              CurrentWorkScheduleType = o.PayPeriodWorkScheduleType.HasValue ? (WorkScheduleType?)WorkSchedule.ConvertToWorkScheduleType(o.PayPeriodWorkScheduleType.Value) : null,
              DodPersonId = o.DodPersonId,
              EmailAddress = new MailAddress(o.EmailAddress),
              IsActive = o.IsActive,
              IsSupervisor = o.IsSupervisor,
              IsAlternateLeaveApprover = o.IsAlternateLeaveApprover,
              IsTravelApprover = o.IsTravelApprover,
              IsTravelCompensationApprover = o.IsTravelCompensationApprover,
              IsTimekeeper = o.IsTimekeeper,
              LastLogOn = o.LastLogOn,
              DateTimeLastAcceptedCertificationStatement = o.DateTimeLastAcceptedCertificationStatement, 
              ResidenceToWorksiteOneWayCommuteTime = o.ResidenceToWorkSiteOneWayCommuteTime.HasValue ? (TimeSpan?)TimeSpan.FromMinutes((double)o.ResidenceToWorkSiteOneWayCommuteTime) : null , 
              Organization = OrganizationManager.GetOrganization(o.OrganizationId),
              OrganizationalUnitId = o.OrganizationalUnitId,
              OrganizationalUnitTitle = o.OrganizationalUnitTitle,
              PermanentWorkScheduleId = o.PayPeriodPermanentWorkScheduleId,
              PermanentWorkScheduleEffectivePayPeriod = o.PayPeriodPermanentWorkScheduleEffectivePayPeriodStartDate.HasValue ? PayPeriod.GetPayPeriod(o.PayPeriodPermanentWorkScheduleEffectivePayPeriodStartDate.Value) : null,
              PermanentWorkScheduleType = o.PayPeriodPermanentWorkScheduleType.HasValue ? (WorkScheduleType?)WorkSchedule.ConvertToWorkScheduleType(o.PayPeriodPermanentWorkScheduleType.Value) : null,
              Name = new PersonName()
              {
                LastName = o.LastName,
                FirstName = o.FirstName,
                MiddleNameOrInitial = o.MiddleNameOrInitial,
                Cadency = o.Cadency
              }
              
            });
        }
      }
    

    Now the issue is, You will see a few ternary statements in the .select clause. I recently added the ResidenceToWorksiteOneWayCommuteTime field to the employee object. If I place it in the GetEmployeesQuery without the ternary statement it works just fine OR if I get rid of BOTH ternary statements dealing with WorkSchedule it works fine. However with all three in place I get the following error:

    "System.ArgumentException was unhandled by user code
      Message="The argument 'value' was the wrong type. Expected 'System.Nullable`1[Itst.Tms.Schedule.WorkScheduleType]'. Actual 'System.Nullable`1[System.TimeSpan]'."
      Source="System.Data.Linq""

    I cannot see what Linq2Sql is doing as it never hits the database, it simply fails.

     

    Suggestions?  Clues? HALP

     

    Monday, February 14, 2011 8:32 PM

Answers

  • No, all properties associated with ternary statements are in fact nullable. I worked around the issue by creating a private static method that took care of doing the ternary work for the WorkScheduleType values which removed the ternary from the sql query build. It strikes me as somewhat hokey to have to do that but... that is what I did... it now works... but... something should be done about that fail...  Here is what the final code looks like(I didn't put in the new method, but all I did was to move the ternary logic into there... )

     

    return ctx.SelectEmployeesDynamic(
            criteria.EmployeeId,
            criteria.OrganizationId,
            criteria.OrganizationalUnitId,
            criteria.DodPersonId,
            criteria.IsActive,
            criteria.IsAlternateLeaveApprover,
            criteria.IsTravelCompensationApprover,
            criteria.IsSupervisor,
            criteria.IsTravelApprover,
            criteria.IsTimekeeper,
            criteria.PayPeriod.StartDate,
            criteria.CoworkerEmployeeId,
            criteria.TimekeeperEmployeeId,
            (int?)criteria.CurrentWorkScheduleType,
            (int?)criteria.PermanentWorkScheduleType).Select(o => new Employee
            {
              Id = o.EmployeeId,
              CurrentWorkScheduleId = o.PayPeriodWorkScheduleId,
              CurrentWorkScheduleEffectivePayPeriod = o.PayPeriodWorkScheduleId.HasValue ? (o.PayPeriodPermanentWorkScheduleId.HasValue && o.PayPeriodWorkScheduleId == o.PayPeriodPermanentWorkScheduleId ? PayPeriod.GetPayPeriod(o.PayPeriodPermanentWorkScheduleEffectivePayPeriodStartDate.Value) : PayPeriod.GetPayPeriod(criteria.PayPeriod.StartDate)) : null,
              CurrentWorkScheduleType = GetWorkScheduleTypeFromDatabaseObject(o.PayPeriodWorkScheduleType),
              DodPersonId = o.DodPersonId,
              EmailAddress = new MailAddress(o.EmailAddress),
              IsActive = o.IsActive,
              IsSupervisor = o.IsSupervisor,
              IsAlternateLeaveApprover = o.IsAlternateLeaveApprover,
              IsTravelApprover = o.IsTravelApprover,
              IsTravelCompensationApprover = o.IsTravelCompensationApprover,
              IsTimekeeper = o.IsTimekeeper,
              LastLogOn = o.LastLogOn,
              DateTimeLastAcceptedCertificationStatement = o.DateTimeLastAcceptedCertificationStatement, 
              ResidenceToWorksiteOneWayCommuteTime = o.ResidenceToWorkSiteOneWayCommuteTime.HasValue ? (TimeSpan?)TimeSpan.FromMinutes((double)o.ResidenceToWorkSiteOneWayCommuteTime.Value) : null, 
              Organization = OrganizationManager.GetOrganization(o.OrganizationId),
              OrganizationalUnitId = o.OrganizationalUnitId,
              OrganizationalUnitTitle = o.OrganizationalUnitTitle,
              PermanentWorkScheduleId = o.PayPeriodPermanentWorkScheduleId,
              PermanentWorkScheduleEffectivePayPeriod = o.PayPeriodPermanentWorkScheduleEffectivePayPeriodStartDate.HasValue ? PayPeriod.GetPayPeriod(o.PayPeriodPermanentWorkScheduleEffectivePayPeriodStartDate.Value) : null,
              PermanentWorkScheduleType = GetWorkScheduleTypeFromDatabaseObject(o.PayPeriodPermanentWorkScheduleType),
              Name = new PersonName()
              {
                LastName = o.LastName,
                FirstName = o.FirstName,
                MiddleNameOrInitial = o.MiddleNameOrInitial,
                Cadency = o.Cadency
              }
            });
    
    Thursday, February 17, 2011 7:53 PM

All replies

  • Hi Bill,

    >>ResidenceToWorksiteOneWayCommuteTime = o.ResidenceToWorkSiteOneWayCommuteTime.HasValue ? (TimeSpan?)TimeSpan.FromMinutes((double)o.ResidenceToWorkSiteOneWayCommuteTime) : null ,
    //here you may loss .value

    ============================================

    ResidenceToWorksiteOneWayCommuteTime = o.ResidenceToWorkSiteOneWayCommuteTime.HasValue ? (TimeSpan?)TimeSpan.FromMinutes((double)o.ResidenceToWorkSiteOneWayCommuteTime.Value) : null ,

    ============================================

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 15, 2011 6:07 AM
    Moderator
  • Just tried that, same issue... :(
    Tuesday, February 15, 2011 12:59 PM
  • Hi Bill,

    Thanks for your feedback.

    >>>>>> Message="The argument 'value' was the wrong type. Expected 'System.Nullable`1[Itst.Tms.Schedule.WorkScheduleType]'. Actual 'System.Nullable`1[System.TimeSpan]'."

    The exception may relates some properties are not Nullable properties, Please check each property when you use ternary.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 17, 2011 3:15 AM
    Moderator
  • No, all properties associated with ternary statements are in fact nullable. I worked around the issue by creating a private static method that took care of doing the ternary work for the WorkScheduleType values which removed the ternary from the sql query build. It strikes me as somewhat hokey to have to do that but... that is what I did... it now works... but... something should be done about that fail...  Here is what the final code looks like(I didn't put in the new method, but all I did was to move the ternary logic into there... )

     

    return ctx.SelectEmployeesDynamic(
            criteria.EmployeeId,
            criteria.OrganizationId,
            criteria.OrganizationalUnitId,
            criteria.DodPersonId,
            criteria.IsActive,
            criteria.IsAlternateLeaveApprover,
            criteria.IsTravelCompensationApprover,
            criteria.IsSupervisor,
            criteria.IsTravelApprover,
            criteria.IsTimekeeper,
            criteria.PayPeriod.StartDate,
            criteria.CoworkerEmployeeId,
            criteria.TimekeeperEmployeeId,
            (int?)criteria.CurrentWorkScheduleType,
            (int?)criteria.PermanentWorkScheduleType).Select(o => new Employee
            {
              Id = o.EmployeeId,
              CurrentWorkScheduleId = o.PayPeriodWorkScheduleId,
              CurrentWorkScheduleEffectivePayPeriod = o.PayPeriodWorkScheduleId.HasValue ? (o.PayPeriodPermanentWorkScheduleId.HasValue && o.PayPeriodWorkScheduleId == o.PayPeriodPermanentWorkScheduleId ? PayPeriod.GetPayPeriod(o.PayPeriodPermanentWorkScheduleEffectivePayPeriodStartDate.Value) : PayPeriod.GetPayPeriod(criteria.PayPeriod.StartDate)) : null,
              CurrentWorkScheduleType = GetWorkScheduleTypeFromDatabaseObject(o.PayPeriodWorkScheduleType),
              DodPersonId = o.DodPersonId,
              EmailAddress = new MailAddress(o.EmailAddress),
              IsActive = o.IsActive,
              IsSupervisor = o.IsSupervisor,
              IsAlternateLeaveApprover = o.IsAlternateLeaveApprover,
              IsTravelApprover = o.IsTravelApprover,
              IsTravelCompensationApprover = o.IsTravelCompensationApprover,
              IsTimekeeper = o.IsTimekeeper,
              LastLogOn = o.LastLogOn,
              DateTimeLastAcceptedCertificationStatement = o.DateTimeLastAcceptedCertificationStatement, 
              ResidenceToWorksiteOneWayCommuteTime = o.ResidenceToWorkSiteOneWayCommuteTime.HasValue ? (TimeSpan?)TimeSpan.FromMinutes((double)o.ResidenceToWorkSiteOneWayCommuteTime.Value) : null, 
              Organization = OrganizationManager.GetOrganization(o.OrganizationId),
              OrganizationalUnitId = o.OrganizationalUnitId,
              OrganizationalUnitTitle = o.OrganizationalUnitTitle,
              PermanentWorkScheduleId = o.PayPeriodPermanentWorkScheduleId,
              PermanentWorkScheduleEffectivePayPeriod = o.PayPeriodPermanentWorkScheduleEffectivePayPeriodStartDate.HasValue ? PayPeriod.GetPayPeriod(o.PayPeriodPermanentWorkScheduleEffectivePayPeriodStartDate.Value) : null,
              PermanentWorkScheduleType = GetWorkScheduleTypeFromDatabaseObject(o.PayPeriodPermanentWorkScheduleType),
              Name = new PersonName()
              {
                LastName = o.LastName,
                FirstName = o.FirstName,
                MiddleNameOrInitial = o.MiddleNameOrInitial,
                Cadency = o.Cadency
              }
            });
    
    Thursday, February 17, 2011 7:53 PM
  • Hi Bill,

    >>  PermanentWorkScheduleType = GetWorkScheduleTypeFromDatabaseObject(o.PayPeriodPermanentWorkScheduleType),

    >>   PermanentWorkScheduleType = o.PayPeriodPermanentWorkScheduleType.HasValue ? (WorkScheduleType?)WorkSchedule.ConvertToWorkScheduleType(o.PayPeriodPermanentWorkScheduleType.Value) : null,

    Do you mean you just change here?

    It seems that PayPeriodPermanentWorkScheduleType is not nullable type from your code.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 18, 2011 9:20 AM
    Moderator