如何将Class转换为IEnumerable?

Kav*_*tha -2 c# ienumerable list

是否可以将类转换为IEnumerable

 private SeriesMapping CreateSeriesMapping(string category, string value, string stackKey, CustomObject itemsSource)
    {
        var data= (IEnumerable)itemsSource;
        SeriesMapping successMapping = new SeriesMapping() { SeriesDefinition = new StackedBar100SeriesDefinition() { StackGroupName = stackKey } };
        successMapping.ItemMappings.Add(new ItemMapping(category, DataPointMember.XCategory));
        successMapping.ItemMappings.Add(new ItemMapping(value, DataPointMember.YValue));
        successMapping.ItemsSource = data; // after conversion i did not any data.

     // successMapping.ItemsSource = itemsSource; //if i use this i did not get error But while opening the chart Null Reference exception is coming. 
        return successMapping;
    }
Run Code Online (Sandbox Code Playgroud)

这就是我认为班级对象的方式....

slo*_*oth 5

如果你想创建一个单元素集合(你的问题不是很清楚),你可以简单地使用一个数组:

IEnumerable<CustomObject> data = new []{ itemsSource };   
Run Code Online (Sandbox Code Playgroud)