locked
IEnumerable query with internal IEnumerable queries RRS feed

  • Question

  •  

    I am working with some dynamic xml data that has a collection of items.  Each item can have its own collection of items.

    <Item>

    <Tag>Data</< FONT>Tag>

    <ParameterList>

    <Parameter>1</< FONT>Parameter>

    <Parameter>2</< FONT>Parameter>

    </< FONT>ParameterList>

    </< FONT>Item>

    <Item>

    <Tag>Data1</< FONT>Tag>

    <ParameterList>

    <Parameter>3</< FONT>Parameter>

    </< FONT>ParameterList>

    </< FONT>Item>

    I have the following struct defined:

     

    Code Snippet

    public struct Item

    {

    public IEnumerable<Parameter> ParameterList;

    public string TagName;

    }

     

     

     

    And I use the following query

     

    Code Snippet

    query = from parmList in xDoc.Descendants("ItemList").First().Elements("Item")

    select new Item

    {

    TagName = (string)parmList .Element("Tag"),

    ParameterList= from parm in parameterList .Element("ParameterList").Elements("Parameter")

    select new Parameter

    {

    //

    }

    };

     

    List<Item> itemList = query.ToList();

     

     

    Everything above works but both the query and ParameterList are IEnumerable.  I can use the casting on query to convert it into a List but how do you convert the internal ParameterList IEnumerable into a List?  I actually want ParameterList in the struct to be a List<Parameter> rather than IEnumerable. 

     

    Thanks

     

    Monday, June 16, 2008 5:31 PM

Answers

  • Just put the nested query in parens and use .ToList() on it:

     

    Code Snippet

    query =

        from parmList in xDoc.Descendants("ItemList").First().Elements("Item")

        select new Item {

            TagName = (string)parmList .Element("Tag"),

            ParameterList= (

                from parm in parameterList .Element("ParameterList").Elements("Parameter")

                select new Parameter {

                    //

                }).ToList()

        };

     

     

     

    Anders

    Tuesday, June 17, 2008 5:14 PM

All replies

  • in the struct, modify the IEnumerable<Parameter> ParameterList to:

     

    IList<Parameter> ParameterList;

     

    if you want to cast it outside the struct, you can do something like this:

     

    IList<Parameter> parameters = ParameterList.ToList();

    Monday, June 16, 2008 7:37 PM
  • Thanks for the reply Yazeed.  When I try to use what you suggest I have the same issue I get when doing anything in the struct other than an IEnumerable.  How do i put that cast on the internal select?

     

    Error 3 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IList'. An explicit conversion exists (are you missing a cast?)

     

    I managed to do some cumbersome magic to make it work by creating a class with some accessors that do the conversion but it is certainly not as clean or elegant as I would like.

     

    Code Snippet

    public class Item

    {

    public string TagName { get; set; }

    public IEnumerable<Parameter> ParmList {

    set

    {

    ParameterList = new List<Parameter>(value.Cast<Parameter>());

    }

    }

    public List<Parameter> ParameterList { get; set; }

    }

     

     

     

    Maybe I'm missing something but it should be as easy on the internal collections as the external to convert it from an IEnumerable so that I can actually use it.  This is a starting point that can then have items added or removed and then stored right back out to a file or sent over the network.

    Tuesday, June 17, 2008 3:15 PM
  • Hi Mark,

     

    I'm not sure what you have tried so far as you stated earlier this should be easy and straight forward, i have done tons of casting in my projects and never faced such an issue, anyway, if you could please paste me the code you have tried with a full example so i can run it and test it on my box, meanwhile, you can look at these examples:

     

    1.to convert any collection that implements IEnumerable to a list ,simply use this:

     

    //Getting a collection of IEnumerable

    IEnumerable<int> MyCollection = new int[] { 1, 2, 3 };

    //Converting it to a List

    IList<int> myList = MyCollection.ToList();

    //performing a query againt the list.

    var q = myList.Where(m => m == 1);

     

    2. To Cast Items inside legacy collections inorder to use IQueryable functionality, you can use Cast<> or OfType. this can be used as an example for :

     

    IList<ListItem> selectedItems = ListBox.Items.Cast<ListItem>().Where(i => i.Selected == true).ToList();

    OR

     

    IList<ListItem> selectedItems = ListBox.Items.OfType<ListItem>().Where(i => i.Selected == true).ToList();

    Tuesday, June 17, 2008 4:17 PM
  • Just put the nested query in parens and use .ToList() on it:

     

    Code Snippet

    query =

        from parmList in xDoc.Descendants("ItemList").First().Elements("Item")

        select new Item {

            TagName = (string)parmList .Element("Tag"),

            ParameterList= (

                from parm in parameterList .Element("ParameterList").Elements("Parameter")

                select new Parameter {

                    //

                }).ToList()

        };

     

     

     

    Anders

    Tuesday, June 17, 2008 5:14 PM
  • Thanks for the responses and the help.  I figured it was something simple, I just didn't try the ( ).

     

    Mark

     

    Wednesday, June 18, 2008 7:56 PM