First off, I’m just starting to mess around with LINQ, and I don’t really understand it.
Anyways, what I’m trying to do is add to a List with LINQ and the .Where statement.
This is what I’ve tried:
List<object> FirstList = new List<object>();
List<object> SecondList = new List<object>();
Listener.Clients.Where(x => (x.Value.Authenticated) ? FirstList.Add(x.Value.UserID) : SecondList.Add(x.Value.UserID));
Listener is a socket wrapper class, and Clients is an array of a class, Client.
In that Client class there are a few values that I need, and I want to check if that client is authenticated (logged in), and if it is, add it to the first list, and if it’s not, add it to the second list, however it seems that the ?: operator must have a return value, but adding to a list does not return anything.
I hope I explained that good; I’m not the best with words.
You are trying to create two (or more) lists with one enumeration. Consider using
ToLookupinstead:When an unknown key is sent to the lookup, it will return an empty collection.
You want UserIds instead of Clients, so use the overload of ToLookup that selects values.