score:9
Accepted answer
just use a regular for
loop - that's the simplest way of modifying the collection:
for (int i = 0; i < list.count; i++)
{
string url = list[i];
if (!url.startswith("http://"))
{
list[i] = "http://" + url;
}
}
if you're happy to create a new collection, it's simple:
var modifiedlist = list.select(url => url.startswith("http://") ? url : "http://" + url)
.tolist();
score:1
another way to do this by using union
is:
var result =
list.where(url => url.startswith("http://"))
.union(list.where(url => !url.startswith("http://")).select(url => $"http://{url}"))
.tolist();
score:3
yourlist.where(_ => !_.startswith("http://")).tolist().foreach(_ => _.insert(0, "http://"));
Source: stackoverflow.com
Related Query
- Conditional update strings in collection with LINQ
- Linq to update a collection with values from another collection?
- Update property in object collection with linq
- Mapping collection of strings with Entity Framework or Linq to SQL
- Create a tree structure in linq with a single list source with parent - child as strings of an object
- Convert a collection of objects with a string property to an enumeration of those strings using LINQ
- Merge a collection with duplicate objects and update some properties with LINQ
- Find a certain value from a source collection in a target collection of different type with linq
- Linq conditional select with null member collection
- Update property in collection with LINQ
- Group by with linq to update collection
- Update all objects in a collection using LINQ
- Paging a collection with LINQ
- Most efficient way to update with LINQ to SQL
- Sorting an observable collection with linq
- Update XML with C# using Linq
- query a sub-collection of a collection with linq
- linq how to select a parent with a child collection that contains one or many of an array (or list) of values
- Removing characters from strings with LINQ
- How to update an element with a List using LINQ and C#
- LINQ sum collection of items to return object with results (multiple columns)
- Filtering collection with LINQ
- How can I iterate over a collection and change values with LINQ extension methods?
- Filter a collection with LINQ vs CollectionView
- C# Linq - get objects with not empty collection
- How do I use LINQ to reduce a collection of strings to one delimited string?
- LINQ WHERE method alters source collection
- Using LINQ to generate a random size collection filled with random numbers
- Check if a value is in a collection with LINQ
- How do I convert IEnumerable to Collection with linq
More Query from same tag
- Inconsistencies in data retrieval from csv file
- T-Sql to linq conversion issues (order by)
- Select a dictionary with certain properties of a class
- Unique ticket numbers for software support system
- Removing sequential repeating items from List<T> using linq
- Binding an Enum to LINQ and SelectListItem
- c# - linq to xml: read a xml config file
- Fetching records by date with only day part comparison using nhibernate
- Filtering selected JSon fields in the DB side
- Returning a substring from the value field of an xml doc using Linq to XML
- LINQ Query Returning Multiple Copies Of First Result
- How do I update a model in ASP.NET MVC LINQ to SQL from another object?
- Grouping a sequence into two sets, then printing from first set based on num of elements in second set using LINQ
- Why is the LINQ "apply-to-all" method named Select?
- using linq query to find value that differs from previously found value
- Simplest way to form a union of two lists
- how to add function inside select query
- Linq to xml, when child nodes meet a certain condition
- LINQ Lambda Order in writing the query
- Assigning max values from a dictionary
- C# LINQ append multiple items rather than returning its array
- Using generics instead of casting for AsEnumerable
- How to add Linq support in SQL CLR
- LINQ to SQL use of => operand on String not allowed
- Convert SQL query to linq lambda Entity Framework Core
- Assign a user to AspNetUserRole in Console Line Application C#
- Linq Regex for whole word search
- cannot convert type decimal to double
- How do I return multiple xml elements/ attributes with linq, and create objects with them?
- How do I update a field of a record using linq