locked
How to add an IApiResponse to an IEnumerable RRS feed

  • Question

  • User941905567 posted

    I have an IEnumerable. I would like to add to it after I make an Api call. Can I do this ? Or do I need to do something else? Any advice or direction would be most appreciated.

    --Jason

    //My IEnumerable    
    IEnumerable<MoratoriumLocationDto> moralocs = null;
    
    foreach (MoratoriumDto mora in moratoriums) {
                 
    //My Api call   
                    IApiResponse<IEnumerable<MoratoriumLocationDto>> apiResponsemoratoriumlocations = await _moratoriumService.GetLocationsByMoratoriumID(mora.Id);
                   
                    //How do I add to the IEnumerable ? This results in a compile error
                    moralocs = moralocs.Add(apiResponsemoratoriumlocations)
                }

    Tuesday, April 14, 2020 8:34 PM

Answers

  • User303363814 posted

    The IEnumerable interface has a single method which can be used to enumerate (step through) a collection.  There is no member of the interface which allows any form of insertion/ appending/deletion.

    If you are creating something that you wish to add to then an interface like IList is more appropriate as it provides methods that let you insert/add to the object.  Something like

    IList<MoratoriumLocationDto> moralocs = new List<MoratoriumLocationDto>();

    would be more appropriate.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 15, 2020 2:22 AM
  • User-1330468790 posted

    Hi Jazzcatone,

     

    As @PaulTheSmith said, the IEnumerable<T> interface is designed to traverse the collection.

    You have to declare a collection which has implemented an interface (e.g. ICollection/IList/ISet) that has defined a method "Add".

    That way, you would be able to add the returned value of the API call to the collection.

     

    For example,

    public async Task YourMethod()
    {
    //Your Collection to store the value List<MoratoriumLocationDto> moralocs = new List<MoratoriumLocationDto>(); foreach (MoratoriumDto mora in moratoriums)
    { //Your Api call, note that here the returned value should in type of "MoratoriumLocationDto" MoratoriumLocationDto apiResponse = await _moratoriumService.GetLocationsByMoratoriumID(mora.Id); //Add item to the collection moralocs.Add(apiResponse); }
    }

      

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 15, 2020 3:43 AM

All replies

  • User303363814 posted

    The IEnumerable interface has a single method which can be used to enumerate (step through) a collection.  There is no member of the interface which allows any form of insertion/ appending/deletion.

    If you are creating something that you wish to add to then an interface like IList is more appropriate as it provides methods that let you insert/add to the object.  Something like

    IList<MoratoriumLocationDto> moralocs = new List<MoratoriumLocationDto>();

    would be more appropriate.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 15, 2020 2:22 AM
  • User-1330468790 posted

    Hi Jazzcatone,

     

    As @PaulTheSmith said, the IEnumerable<T> interface is designed to traverse the collection.

    You have to declare a collection which has implemented an interface (e.g. ICollection/IList/ISet) that has defined a method "Add".

    That way, you would be able to add the returned value of the API call to the collection.

     

    For example,

    public async Task YourMethod()
    {
    //Your Collection to store the value List<MoratoriumLocationDto> moralocs = new List<MoratoriumLocationDto>(); foreach (MoratoriumDto mora in moratoriums)
    { //Your Api call, note that here the returned value should in type of "MoratoriumLocationDto" MoratoriumLocationDto apiResponse = await _moratoriumService.GetLocationsByMoratoriumID(mora.Id); //Add item to the collection moralocs.Add(apiResponse); }
    }

      

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 15, 2020 3:43 AM
  • User941905567 posted

    Thank you so much !

    Wednesday, April 15, 2020 5:23 PM