I have a list where I want to remove last comma from every list item, what is the best way to do it in c# linq
var mylist = new List<colors>{
new Color{
Id=1,
Name="red,blue,green,orange,"
},
new Color{
Id=2,
Name="purple,sky blue, pink, red,"},
new Color{
Id=3,
Name="green, yellow, black,"
},
new Color{
Id=4,
Name="white,black,red, blue,"
}
}
Is there any way to remove last comma from all items in a one go by using linq c#?
CodePudding user response:
Linq is excelent for quering data not for modifying; that's why I suggest a good old for
loop:
for (int i = 0; i < myList.Count; i)
myList[i].Name = myList[i].Name.TrimEnd(',');
or if Name
is init-only and we have to recreate Color
:
for (int i = 0; i < myList.Count; i)
myList[i] = new Color() {
Id = myList[i].Id,
Name = myList[i].Name.TrimEnd(',')
}
Technically, you can use Linq
myList = myList
.Select(color => new Color() {
Id = color.Id,
Name = color.Name.TrimEnd(',')
})
.ToList();
but it's overshoot
CodePudding user response:
Maybe you can change your class Color
definition as follows:
class Color
{
public int Id{get;set;}
private string _name;
public string Name{
get{
return _name;
}
set{
_name = value.TrimEnd(',');
}
}
}
then when you create a list, the comma is auto removed, no need to do anything:
var mylist = new List<colors>{
new Color{
Id=1,
Name="red,blue,green,orange,"
},
new Color{
Id=2,
Name="purple,sky blue, pink, red,"},
new Color{
Id=3,
Name="green, yellow, black,"
},
new Color{
Id=4,
Name="white,black,red, blue,"
}
}
Approach 1: add constructor to your class
public class Color
{
public Color()
{
}
public Color(Color source)
{
this.Id = source.Id;
this.Name = source.Name.TrimEnd(',');
}
public int Id{get;set;}
public string Name{get;set;}
}
void Main()
{
var mylist = new List<Color>{
new Color{
Id=1,
Name="red,blue,green,orange,"
},
new Color{
Id=2,
Name="purple,sky blue, pink, red,"},
new Color{
Id=3,
Name="green, yellow, black,"
},
new Color{
Id=4,
Name="white,black,red, blue,"
}
};
var res = from c in mylist
select new Color(c);
Console.Write(res);
}
Approach 2: add a RemoveComma()
method to your class
public class Color
{
public int Id{get;set;}
public string Name{get;set;}
public Color RemoveComma()
{
this.Name = this.Name.TrimEnd(',');
return this;
}
}
void Main()
{
var mylist = new List<Color>{
new Color{
Id=1,
Name="red,blue,green,orange,"
},
new Color{
Id=2,
Name="purple,sky blue, pink, red,"},
new Color{
Id=3,
Name="green, yellow, black,"
},
new Color{
Id=4,
Name="white,black,red, blue,"
}
};
var res = from c in mylist
select c.RemoveComma();
Console.Write(res);
}
Approach 3: if you dont want to modify the Color
class, define a extension method
public class Color
{
public int Id{get;set;}
public string Name{get;set;}
}
public static class ColorExtensions
{
public static Color RemoveComma(this Color c)
{
c.Name = c.Name.TrimEnd(',');
return c;
}
}
void Main()
{
var mylist = new List<Color>{
new Color{
Id=1,
Name="red,blue,green,orange,"
},
new Color{
Id=2,
Name="purple,sky blue, pink, red,"},
new Color{
Id=3,
Name="green, yellow, black,"
},
new Color{
Id=4,
Name="white,black,red, blue,"
}
};
var res = from c in mylist
select c.RemoveComma();
Console.Write(res);
}