Home > Net >  Casting from a Task<IEnumerable<X>> to a Task<IEnumerable<Y>>
Casting from a Task<IEnumerable<X>> to a Task<IEnumerable<Y>>

Time:12-08

I want to cast a list of items from one object to another as follows:

public async Task<IEnumerable<TaskDetailsEntity>> GetTaskDetails(int personnelSK)
    {            
        //IEnumerable<TaskDetailsEntity> TaskDetailsEntitiesList = await GetKTaskDetails(personnelSK).Select(a => (KTaskDetailsEntity)a).ToList();
        IEnumerable<TaskDetailsEntity> TaskDetailsEntitiesList = await GetKTaskDetails(personnelSK).Cast<KTaskDetailsEntity>().ToList();
        
        return TaskDetailsEntitiesList;
    }

    public async Task<IEnumerable<KTaskDetailsEntity>> GetKTaskDetails(int personnelSK)
    {
        List<ParameterInfo> parameters = new List<ParameterInfo>();                      
        if (personnelSK != 0)
        {
            parameters.Add(new ParameterInfo() { ParameterName = "@PI_PERSONNEL_SK", ParameterValue = personnelSK });
        }
        IEnumerable<KTaskDetailsEntity> KTaskDetailsEntitiesList = await _SQLHelper.GetRecords<KTaskDetailsEntity>("[xx].[GET_TASK_DATA]", parameters);          
        return KTaskDetailsEntitiesList;
    }

But with this, I'm getting below error in the 1st method:

'Task<IEnumerable>' does not contain a definition for 'Cast' and no accessible extension method 'Cast' accepting a first argument of type 'Task<IEnumerable>' could be found (are you missing a using directive or an assembly reference?)

CodePudding user response:

It looks like you are trying to cast a list of items from one object type to another object type. In your code, you are calling the GetKTaskDetails method and passing in a personnelSK integer value. This method returns a list of KTaskDetailsEntity objects, which you are trying to convert to a list of TaskDetailsEntity objects.

To do this, you can use the Cast method, which is available on any IEnumerable or IEnumerable object. The Cast method takes a generic type parameter that specifies the type that you want to cast the elements of the list to. In your code, you are using the Cast method to convert the list of KTaskDetailsEntity objects to a list of TaskDetailsEntity objects.

Once you have called the Cast method, you can use the ToList method to convert the resulting IEnumerable object to a List object. This will give you a list of TaskDetailsEntity objects that you can then return from your GetTaskDetails method.

Here is the updated code:

public async Task<IEnumerable<TaskDetailsEntity>> GetTaskDetails(int personnelSK)
{            
    // Convert the list of KTaskDetailsEntity objects to a list of TaskDetailsEntity objects.
    IEnumerable<TaskDetailsEntity> TaskDetailsEntitiesList = await GetKTaskDetails(personnelSK)
        .Cast<KTaskDetailsEntity>()
        .ToList();
        
    return TaskDetailsEntitiesList;
}

public async Task<IEnumerable<KTaskDetailsEntity>> GetKTaskDetails(int personnelSK)
{
    List<ParameterInfo> parameters = new List<ParameterInfo>();                      
    if (personnelSK != 0)
    {
        parameters.Add(new ParameterInfo() { ParameterName = "@PI_PERSONNEL_SK", ParameterValue = personnelSK });
    }
    IEnumerable<KTaskDetailsEntity> KTaskDetailsEntitiesList = await _SQLHelper.GetRecords<KTaskDetailsEntity>("[xx].[GET_TASK_DATA]", parameters);          
    return KTaskDetailsEntitiesList;
}

CodePudding user response:

Just wrap with braces your await-ed result:

public async Task<IEnumerable<TaskDetailsEntity>> GetTaskDetails(int personnelSK)
{            
    //IEnumerable<TaskDetailsEntity> TaskDetailsEntitiesList = await GetKTaskDetails(personnelSK).Select(a => (KTaskDetailsEntity)a).ToList();
    IEnumerable<TaskDetailsEntity> TaskDetailsEntitiesList = (await GetKTaskDetails(personnelSK)).Cast<TaskDetailsEntity>().ToList();
    
    return TaskDetailsEntitiesList;
}

UPDATE

Actually no need in Cast<> call here due to IEnumerable<T> covariance, so, final one is:

public async Task<IEnumerable<TaskDetailsEntity>>GetTaskDetails(int personnelSK)
{        
  return (await GetKTaskDetails(personnelSK)).ToList();
}
  • Related