-2

I have a method MapResults which returns IEnumerable<T> and I have a list temp. I want to add returnVal and temp lists and return IEnumerable<T>.

public class RetrieverClass<T, TEntity> : IRetriever<T, TEntity> 
    where T : BaseFileEntity 
    where TEntity : class, IEntity<int>
{
    public async Task<IEnumerable<T>> TestMethod(IEnumerable<Item> items)
    {
        var returnVal =  MapResults(result).ToList();

        List<TestEntity> temp = new List<TestEntity>();

        foreach (var testNo in testNos)
        {
            TestEntity test = CreateTestEntity(testNo);
            temp.Add(test);
        }

        returnVal.AddRange(temp);
    }

    private IEnumerable<T> MapResults(IEnumerable<TEntity> results)
    {
        return results.Select(x => _repository.ObjectMapper.Map<T>(x));
    }
}

public class TestEntity : BaseFileEntity
{
}

But its giving error on line returnVal.AddRange(temp).

cannot convert from System.Collections.Generic.List<TestEntity> to System.Collections.Generic.IEnumerable<T>

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197

1 Answers1

0

As suggested by @Camilo Terevinto and @mjwills return (IEnumerable<T>)retVal; had given me a hint to proceed to solve my issue. I'm posting my answer so that it can help others. Thanks everyone for your help, your comments really helped.

public async Task<IEnumerable<T>> TestMethod(IEnumerable<MasterDataFileOperationItem> items)
{

    var ids = items.Select(i => i.Id).ToArray().Select(x => x.ToString()).ToArray();

    var result = await _repository.GetAll().Where(x => ids.Contains(x.Id)).ToListAsync();

    var returnVal = MapResults(result).ToList();

    var temp = CreateTestEntity(testNos);

    returnVal.AddRange(temp);

    return returnVal;
}

public IEnumerable<T> CreateTestEntity(string[] testNos)
{
    List<TestEntity> ls = new List<TestEntity>();
    foreach (var testNo in testNos)
    {
        var TestEntity = new TestEntity
        {
            TestNo = testNo,
        };

        ls.Add(TestEntity);
    }

    return (IEnumerable<T>) ls;
}
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197