c# - Filter list of objects based on condition -
public class car { public string name {get;set;} public string color {get;set;} } car1 = new {"ford", "white"} car2 = new {"ford fiesta", "blue"} car3 = new {"honda city", "yellow"} car4 = new {"honda", "white"} var carobj = new list<car>(); carobj.add(car1); carobj.add(car2); carobj.add(car3); carobj.add(car4);
i need filter output based on car names, if name subset of names present, remove them.
output:
car2 = new {"ford fiesta", "blue"} car3 = new {"honda city", "yellow"}
here writing this, not giving me desired output.
// remove duplicare records. var carobjnew = new list<car>(); carobjnew = carobj; carobj.removeall(a => carobjnew.any(b => a.name.contains(b.name)));
any on how fix this.
as pointed out in comment have simple mistake. need remove item if list has item contains first item substring:
carobj.removeall(a => carobj.any(b => b.name.tolower().contains(a.name.tolower()) && b.name.length > a.name.length));
if want compare stringcomparison
, swap contains
indexof(string value, stringcomparison comparisontype) > 0
Comments
Post a Comment