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?
Re: LINQ query to remove a single value from a set while retaining another value
i don't do linq really , but i as understand the basic principal
i keep wanting to but never seem to get around to playing with it
so anyone can jump in and correct me if im wrong here
i believe the where clause acts just like your
if ( condition == true) // ( where(condition)== true)
{ select it er add it to a list of items ur going to do something with like delete }
then its implicitly added to a new list of items that match a criteria
so its basically syntax sugar
around ifs and stuff , to make things more standardized for searches
prolly a simple linq tutorial with just that in mind
will then make a lot more sense
here's a post on another forum that almost mirrors this with a answer'
http://stackoverflow.com/questions/4...-with-criteria
here's a tutorial were i believe there are videos on the site as well
http://visualcsharptutorials.com/linq
this one is like individual linq keyword console tests with output of the code
http://www.java2s.com/Tutorials/CSha...orial_LINQ.htm
here's a tutorial based on a music player with lists of songs and music artist
were he uses linq to do searches http://www.csharp-station.com/Tutorial/Linq/Lesson01
Re: LINQ query to remove a single value from a set while retaining another value
Nope. Just create a temp list, then swap it back
Re: LINQ query to remove a single value from a set while retaining another value
I believe I understand the basic idea of LINQ; it seems very similar to writing a SQL database query. Writing a query to eliminate known things is easy and straight forward. That is not what I'm trying to do here. I'm in a situation where I want to retain something specific, and in addition eliminate just one arbitrary item. It is not clear to me how to do that. The links above seem to give information for writing generic queries or how to handle the straight forward case of eliminating known items.
I'm not sure; perhaps what I'm trying to do does not lend itself well to the query structure, and in this case, maybe writing the loop is the better approach, as the query might result in something that is complex and difficult to understand.
Re: LINQ query to remove a single value from a set while retaining another value
well im no expert
i spent some time attempting to do this using linq but couldn't find a way
at the end of my efforts i think
your original solution is better
if its over a network there are other considerations though
i did find a excellent linq reference page though
http://code.msdn.microsoft.com/101-L...mples-3fb9811b
on further thought a delete is a request
rather then a query so it sort of makes sense
not to be able to do so within a query
maybe in the first place just query all non or delete-able items
and remove any sort of delete button when they are worked on
Code:
static void Main(string[] args)
{
Program I = new Program();
I.TestLinq01();
}
public void TestLinq01()
{
listOfuInts = new List<uint>{ 1, 2, 3, 4, 5 };
ListToConsole("via linq before", listOfuInts);
var query = // var is of type ienumerable from linq
from n in listOfuInts // loop
where n != specialValue // conditional
select n; // action
listOfuInts = query.ToList<uint>(); // convert back to uint
ListToConsole("after", listOfuInts);
}
public void ListToConsole<T>(string msg,List<T> list)
{
Console.WriteLine(Environment.NewLine + msg);
foreach (var obj in list)
{
Console.WriteLine(obj);
}
Console.WriteLine(Environment.NewLine);
}
the real pain is that the enumerable changes when you perform a delete
this was my looping solution for a delete all but special values
in truth i don't really use linq much i find it far more difficult
then just straight coding something
Code:
// remove all not with a special value
public void TestLoop02()
{
listOfuInts = new List<uint> { 1, 2, 3, 4, 5 };
ListToConsole("via loop02 before", listOfuInts);
int index = 0;
while (index < listOfuInts.Count)
{
if (listOfuInts.ElementAt(index) != specialValue)
{
listOfuInts.RemoveAt(index);
}
else
{
index++;
}
}
ListToConsole("via loop02 after remaining values not removed", listOfuInts);
}
but linq is more for querys so...
Re: LINQ query to remove a single value from a set while retaining another value
Try (because I haven't tested it):
Code:
values.RemoveAll(x => x == specialValue);
Re: LINQ query to remove a single value from a set while retaining another value
I think I found a way to do it. I was originally looking for way to do it in the query itself by trying to use where statements or select to get the proper result. There did not seem to be a way to do that. However the object returned from the query has methods on it are useful for accomplishing the task. Here is basically what I ended up with:
values.Remove((from amount in values
where amount != specialValue
select amount).First());
Re: LINQ query to remove a single value from a set while retaining another value
@Arjay: That looks like it would remove the special value which was not the desired result.
Re: LINQ query to remove a single value from a set while retaining another value
So here's another thought. If my solution is the best way to do this through LINQ, I'm wondering if it is a better solution than writing the loop. It seems the query will have extract all the non-special values out of the set only to discard one of them.
The loop solution should at most go through two iterations to find something to discard. If the special value is not the first item in the loop the first item will get discarded right away, and if the special value is the first item the loop the second item will get discarded and the loop will end then.
I'm not sure, however, it seems the loop may end up doing less than the query would.
Re: LINQ query to remove a single value from a set while retaining another value
Quote:
Originally Posted by
tron_thomas
@Arjay: That looks like it would remove the special value which was not the desired result.
okay, did you try?
Code:
values.RemoveAll(x => x != specialValue);