-5
string greeting = "";    
foreach (System.Collections.Generic.ICollection<object> x in greetingRecommendation.Resolution.Values)
    {
           foreach (var y in x)
           {
               greeting = (string)y;
           }
    }

I need to retrieve the "Good Morning" value which is inside "greetingRecommendation.Resolution.Values" I did it as above(using a foreach) and its working. But is there a better way to access that single value. enter image description here

  • 1
    Did you try searching an answer to your question before posting? Your question is trivial. –  Aug 25 '17 at 08:11
  • I did some google and I found that ICollection has the ElementAt method. If you know the index of the value that you want to access then you can invoke the ElementAt method and this suppose to return the value much more quickly then using foreach loop . Note that it is an extension method. –  Aug 25 '17 at 08:14
  • I tried to use ElementAt. but there's no method available – Lakindu Nanayakkara Aug 25 '17 at 08:34
  • Because it is extension. I found it here https://msdn.microsoft.com/en-us/library/92t2ye13(v=vs.110).aspx under the list of extension methods. However you can invoke the CopyTo method to copy 1 value. –  Aug 25 '17 at 08:35

1 Answers1

2

You can use LINQ for querying a collection in a simpler way.

Let's take as an example the following collection:

var objCollection = (ICollection<object>) new List<object>
{
    "Good Morning",
    "Good Afternoon",
    "Good Night"
};

Now we can extract the first, last or an item at a specific index with the following:

var firstItem = objCollection.First(); // returns "Good Morning"
var lastItem = objCollection.Last(); // returns "Good Night"
var itemAtIndex1 = objCollection.ElementAt(1); // returns "Good Afternoon"

There is also OrDefault variants of all the previous. The difference is that First will throw an exception if objCollection has no items, but FirstOrDefault will simply return default(object) (which is null by the way) if objCollection has no items.

For putting an example, this is how OrDefault variants can be used:

var firstItem = objCollection.FirstOrDefault(); // returns "Good Morning"
var lastItem = objCollection.LastOrDefault(); // returns "Good Night"
var itemAtIndex99 = objCollection.ElementAtOrDefault(99); // returns null, because `objCollection` has only three items and the index 99 doesn't exist on it

In the scenario of greetingRecommendation.Resolution.Values, something like the following can be used for retrieving the first item of the collection:

var greeting = greetingRecommendation.Resolution.Values.FirstOrDefault() as string;

Let's assume that Values property is of type ICollection<object>.

That line will ensure that:

  • If Values property has no items, no exception will be thrown, and default(object) would be returned.
  • The first item in Values property will be safely casted to a string, to avoid issues if it contains a value that can not be casted to string. If the value can not be casted, default(string) (which is null) will be returned.
  • The variable greeting will contain either null or a string with the first value of the Values property.

Do not forget to add a using System.Linq; in the using directives to be able to use LINQ.

I've prepared a .NET Fiddle in the following link for seeing such LINQ operations in action:

mishamosher
  • 1,003
  • 1
  • 13
  • 28