5
object selectedDataItem;
MyClass.Inventory inventory;
inventory = (MyClass.Inventory) selectedDataItem;

in inventory we can see the details such as:

Trace.Writeline(inventory.Name + " " + inventory.Place);

You see inventory has inventory.Name, Inventory.Place I want to wrap all of the property inside IEnumerable or ObservableCollection so that I can iterate through all of the inventory at once and not by inventory.Name, inventory.Place etc etc...

How can I make inventory IEnumerable so that I can do something like this :

IEnumerable<MyClass.Inventory> enumerable = (IEnumerable<MyClass.Inventory>) inventory;
enumerable = from x in enumerable where x.Name == inventory.Name select x;

Right now if I do this the error is

Unable to cast object of type 'MyClass.Inventory' to type 'System.Collections.Generic.IEnumerable`1[MyClass.Inventory]'.

momokjaaaaa
  • 1,293
  • 3
  • 17
  • 32
  • Is 'selectedDataItem' a array or single Object ? If you want to convert single object to IEnumerable then, you should implement IEnumerable interface to 'Inventory' class. – Uthaiah May 21 '14 at 05:33
  • 2
    `MyClass.Inventory` doesn't implement `IEnumerable`. and it makes no sense for me why would you expect to cast single item to `IEnumerable`. – Sriram Sakthivel May 21 '14 at 05:34

4 Answers4

14

Just do this:

var enumerable = new [] { inventory };
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • That will compile but makes no sense. – Sriram Sakthivel May 21 '14 at 05:35
  • 3
    The OP didn't ask if it made sense, just how to do it. :-) – Enigmativity May 21 '14 at 05:35
  • 1
    That's the problem, what's the point of using IEnumerable when the data is just ONE object. – Aniket Inge May 21 '14 at 05:37
  • 3
    @Aniket - There is quite definitely a point. `IEnumerable` can have zero or more objects, so defining an enumerable with a single object is useful as it can be filtered down to zero objects. And that's what the OP wants to do. This works even for value types. It then becomes somewhat equivalent to `Nullable`. There are plenty of times when this is useful. – Enigmativity May 21 '14 at 07:20
9

Not sure why do you need this, but once i saw next extension method:

public static class CustomExtensions
{
    public static IEnumerable<T> ToEnumerable<T>(this T input)
    {
        yield return input;
    }
}
Uriil
  • 11,948
  • 11
  • 47
  • 68
5

inventory is an instance of class MyClass.Inventory and its not an enumerable list so that you can cast it directly. If you really want an enumerable with that object then you need to create an empty enumerable and add that object to it.

Update

You have to create a List first and add your object to that list, as,

List<MyClass.Inventory> lst = new List<MyClass.Inventory>();
lst.Add(inventory);

And then you can assign your list to the enumerable, as

IEnumerable<MyClass.Inventory> enumerable = lst;

reference

Community
  • 1
  • 1
Bharadwaj
  • 2,535
  • 1
  • 22
  • 35
1

In general that would be a bad practice. If the data you're dealing with is not iterable, making it an IEnumerable makes no sense.

On the other hand, if you still want to use IEnumerable<T>, use List<T>, store your object in it, and then do the LINQ query!

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • I don't know why this is downvoted, so far this is the answer which addresses the core problem that *underlying type is not iteratable* +1 – Sriram Sakthivel May 21 '14 at 05:41