Reputation: 2548
I have this code which will sometimes just have one string in it and sometimes have two. Basically when it has two strings I want to be able to select the second string as at the moment it is selecting the first string in the list.
Here is my code:
List<string> workGroupIdStringValues = new List<string>();
workGroupIdStringValues = (List<string>)session["WorkGroupIds"];
List<Guid> workGroupIds = workGroupIdStringValues.ConvertAll<Guid>(workGroupIdStringValue => new Guid(workGroupIdStringValue));
So "workGroupIdStringValues" will sometimes have a second string, how can I select the second and not the first when there is two strings. Is it possible, if so how?
Thanks
Upvotes: 0
Views: 275
Reputation: 437634
Use LINQ's workGroupIdStringValues.Last()
to discard all strings but the last one; will work fine if there's just one string.
Update: And then of course you have to adapt the code somewhat:
var workGroupId = new Guid(((List<string>)session["WorkGroupIds"]).Last());
Upvotes: 9
Reputation: 1
There are a few ways of doing this, I think the best is to do
workGroupIdStringValues.Count
will give you the total amount of objects, and
workGroupIdStringValues[1]
will return the second entry in your list.
So something like
if(workGroupIdStringValues.Count() > 1)
mystring = workGroupIdStringValues[1];
else
mystring = workGroupIdStringValues[0];
Upvotes: 0
Reputation: 13882
Instead of
List<Guid> workGroupIds = workGroupIdStringValues.ConvertAll<Guid>(workGroupIdStringValue => new Guid(workGroupIdStringValue));
You can do following
Guid workGroupId = new Guid(workGroupIdStringValues[workGroupIdStringValues.Count-1]));
Upvotes: 0
Reputation: 14682
How about
string workGroupIdStringValue = ((List<string>)session["WorkGroupIds"]).Last();
Upvotes: 0