• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Add list of objects using AddRange alongwith modifying value in each object

Resolved: Add list of objects using AddRange alongwith modifying value in each object

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I am fetching a list of objects from an api but sometimes one of the elements of each object in that list is fetched as null. If that is the case I want to manually add that element since I have that value.
This is what I have in mind.
var result = List<item>;
foreach(obj in objects)
{
    var items = //api call;
    result.AddRange(items.Select(t => t.Name ?? obj.Name));  //Something like this
}
return result;
I would prefer to use AddRange if possible but I’m open to other solutions.

Answer:

Try this.
var result = List<item>;
foreach(obj in objects)
{
    var items = //api call;
    var modified = items.Select(t=>{
      t.Name = obj.Name;
      return t; 
    });
    result.AddRange(modified); 
}
return result;
}

If you have better answer, please add a comment about this, thank you!

addrange c# linq
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: how to get and read an xml file in a zip file using xml.etree

02/04/2023

Resolved: The ‘Access-Control-Allow-Origin’ header contains multiple values ‘*, *’, but only one is allowed. cors error not resolving

02/04/2023

Resolved: SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘crm.email’ doesn’t exist (Connection: mysql, SQL: select count(*) as aggregate from `email`

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.