开发者

Generic IEnumerable

I Have a Generic IEnumerable which contains a list of my data. I need to try and access the data to put some of it into an object.

IEnumerable myData = DynamicDataBuilder.GetDataList(dataS开发者_JAVA技巧etData);
ObservableCollection<Pages> myPages = new ObservableCollection<Pages>();

foreach (object item in myData)
{
    Pages myPage = new Pages
    {
        ID = ??????
    };
}

I cant cast myData as a ObservableCollection as not all the data in myData is of type Pages. I have tried doing ID = item.Id - The item. only shows 'ToString', 'Equals', 'GetHashCode' and 'GetType' ID = item["Id"] - Get error

Any help would be much appreciated

For reference here is the GetDataList method:

    public static IEnumerable GetDataList(DataSetData data)
    {
        if (data.Tables.Count() == 0)
            return null;

        DataTableInfo tableInfo = data.Tables[0];

        System.Type dataType = BuildDataObjectType(tableInfo.Columns, "MyDataObject");

        //ObservableCollection<DataObject> l = new ObservableCollection<DataObject>();

        var listType = typeof(ObservableCollection<>).MakeGenericType(new[] { dataType });
        var list = Activator.CreateInstance(listType);

        XDocument xd = XDocument.Parse(data.DataXML);
        var table = from row in xd.Descendants(tableInfo.TableName)
                    select row.Elements().ToDictionary(r => r.Name, r => r.Value);

        foreach (var r in table)
        {
            var rowData = Activator.CreateInstance(dataType) as DataObject;
            if (rowData != null)
            {
                foreach (DataColumnInfo col in tableInfo.Columns)
                {
                    if (r.ContainsKey(col.ColumnName) && col.DataTypeName != typeof(System.Byte[]).FullName && col.DataTypeName != typeof(System.Guid).FullName)
                        rowData.SetFieldValue(col.ColumnName, r[col.ColumnName], true);
                }
            }
            listType.GetMethod("Add").Invoke(list, new[] { rowData });
        }
        ObservableCollection<DataObject> l = list as ObservableCollection<DataObject>;
        return list as IEnumerable;
    }


Just pull the relevant objects out using the OfType extension method

IEnumerable myData = DynamicDataBuilder.GetDataList(dataSetData);
IEnumerable<Page> PageCollection = myData.OfType<Page>();

Now you should be able to create an ObservableCollection<Page> from your strongly-typed PageCollection object

ObservableCollection<Pages> myPages 
       = new ObservableCollection<Pages>(PageCollection);


I'm not sure I'm completely understanding you, but I think I'd try something like...

IEnumerable myData = DynamicDataBuilder.GetDataList(dataSetData);
ObservableCollection<Pages> myPages = new ObservableCollection<Pages>();
foreach (var item in myData)
{
    MyExpectedDataType data = item as MyExpectedDataType;
    If(data != null)
    { 
        Pages myPage = new Pages
        {
            ID = data.Id
        };
        MyPages.Add(myPage);
    }
}


IEnumerable myData = DynamicDataBuilder.GetDataList(dataSetData);
ObservableCollection<Pages> myPages = new ObservableCollection<Pages>();
foreach (MyType item in myData) {
// ...

You can put the type you want in the foreach constuct. Or use OfType extension as Adam says, in the case of .NET 3.5+


Well, that is because the item in your foreach loop is from object type.

foreach (object item in myData)

Just replace it with var keyword or use the actual type

foreach (var item in myData)

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜