Reputation: 1303
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]'.
Upvotes: 5
Views: 42054
Reputation: 2553
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;
Upvotes: 5
Reputation: 12618
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;
}
}
Upvotes: 9
Reputation: 25695
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!
Upvotes: 1