|
-
August 21st, 2014, 11:12 PM
#1
LINQ query to remove a single value from a set while retaining another value
I have read that it is usually better to use LINQ queries over writing loops. I'm in a situation where I have a HasSet of unsigned integers and the program is allowed to modify the set by removing any one value from the set as long as it is not a certain special value already in the set.
For example if the set contains the values { 1, 2, 3, 4, 5 }, and 3 was the special value the algorithm could remove 1, 2, 4, or 5 from the set.
I have accomplished this behavior by writing code like the following:
HashSet values = new HashSet() { 1, 2, 3, 4, 5 };
uint specialValue = 3;
for (var index = 0; index < values.Count; ++index) {
if (values.ElementAt(index) != specialValue) {
values.Remove(values.ElementAt(index));
break;
}
}
Obviously this is using a loop to accomplish the value removal. How could someone accomplish the same result only a LINQ query?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|