c# 4.0 - Remove only one element from string array using linq -


i have following code:

string[] s = {"one", "two", "three", "two", "four"};  s = s.where(x => x!="two").toarray();  

i want remove "two" once using linq there way this? code tried above removes both "two" elements array.

well, maybe want remove duplicates in general, it's simple:

s = s.distinct().toarray(); 

otherwise can use groupby:

s = s.groupby(str => str).selectmany(g => g.key != "two" ? g : g.take(1)).toarray(); 

this allows duplicates in general, two must unique.


Comments

Popular posts from this blog

php - Embed BBCode within itself not working -

three.js - how to load gltf in three JS -

sql server - T-SQL: Access DB objects in same schema without qualifying? -