none
IEnumerable conversion to Generic IEnumerable RRS feed

  • Question

  • Hi..

    I need to convert standard IEnumerable to Generic IEnumerable.

    My ultimate purpose of converting is to get the outcome of below mentioned line.

     

     

    IEnumerable<object> headers = gvQuotationQueue.Columns.Select(c => c.Header); 
     
    ==================================================================================== 
     
    How can i ? I am trying with   
     
      
     
      
    System.Collections. 
     
     
    IEnumerable  
     
    headers = gvQuotationQueue.Columns; 
     
      
     
     
     
     
    IEnumerable<object> columnsHeaders = headers.Cast<object  
     
    >()  
     
    .Select( s=> s); 
     
      
     
     
      
     
     
     
     
     
    ObservableCollection<PROPOSALRENTALDETAILInfo> rey = columnsHeaders .Select(c => c.Header).ToObservableCollection<PROPOSALRENTALDETAILInfo>(); 
     
     
     
     

     

    

    I have found another way but i want to eliminate loop iterations and need a best way with performance.
    
     public ObservableCollection<string> ColumnsHeader { get; set; } 
            public ObservableCollection<string> ColumnsName { get; set; } 
      
    private void GetColumnsHeader() 
            { 
                System.Collections.IEnumerable headers = gvQuotationQueue.Columns; 
     
                 System.Collections.IEnumerator iterations = headers.GetEnumerator(); 
                 iterations.Reset(); 
               while(iterations.MoveNext()) 
                 { 
                     var columnSpecification = ((GridViewDataColumn)iterations.Current); 
                        ColumnsHeader.Add(columnSpecification.Header.ToString()); 
                        ColumnsName.Add(columnSpecification.UniqueName.ToString());                  
                 }             
     
            }

     

     

    Friday, December 9, 2011 7:18 AM

Answers

  • Looking at your code - it looks like your goal is to fill in two ObservableCollection<string> instances?

     

    If so, the fastest option is likely a simple loop:

    private void GetColumnsHeader() 
    { 
        foreach(GridViewDataColumn columnSpec in gvQuotationQueue.Columns)
        {
            ColumnsHeader.Add(columnSpec.Header.ToString()); 
            ColumnsName.Add(columnSpec.UniqueName.ToString());                  
        }
     }
      
    

    Any solution using LINQ is going to effectively iterate through your collection twice, since it will need to do it once per ObservableCollection.  The above, at least, keeps you down to a single enumeration.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Mehmood Ahmed Saturday, December 10, 2011 6:21 AM
    Friday, December 9, 2011 7:00 PM