ILIST<one type> to ILIST<Other type>

krdeepak123
krdeepak123 used Ask the Experts™
on
Hi,
Please help me asap.
Using C# , I want to convert ILIST from one type to other. i.e. in my code from IList<ReportListAccessGroupDetailsInfo> to  IList<Document>. The below function needs output as IList<Document> But i can get output in TRY block as IList<ReportListAccessGroupDetailsInfo>

My code is
 public IList<Document> GetDocumentsByAccessGroup(Guid accessGroupGuid, string isActive)
        {
1. METHOD
            try
            {

                IList<ReportListAccessGroupDetailsInfo> list = DocumentProvider.GetDocumentsByAccessGroup(accessGroupGuid, isActive);
               
             
            }
            catch (Exception ex)
            {                
                throw new Exception(string.Format("An exception occurred retrieving the list of documents", ex.ToString()));
            }
            return null;
        }

  2. IMPLEMENTATION OF ReportListAccessGroupDetailsInfo CLASS

[Serializable]
    public class ReportListAccessGroupDetailsInfo : ReportListAccessGroupInfo
    {
        private int premierPageId;
        private string reportFileDescription;
        private string reportFileName;

        public int PremierPageId
        {
            get { return premierPageId; }
            set { premierPageId = value; }
        }

        public string ReportFileDescription
        {
            get { return reportFileDescription; }
            set { reportFileDescription = value; }
        }

        public string ReportFileName
        {
            get { return reportFileName; }
            set { reportFileName = value; }
        }
    }


   3. IMPLEMENTATION OF Document CLASS

    public class Document
    {
    }

    public class Documents : List<Document>
    { }
}



Thanks & Regards,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014
Commented:
One class has many properties. other class does not seem to have any properties. How do you want the conversion to work out?
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
To convert one IList<type1> to another IList<type2>, you have to loop through the first list and create new instances of type2, and subsequently add those new instances to the new list. There is no direct casting between two generic Lists.

Author

Commented:
Hi Kaufmed,

can you give me this looping code based on my code please?

Thanks & Regards,
Deepak
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
can you give me this looping code based on my code please?
I can only give you a partial example since it's as CodeCruiser said:

One class has many properties. other class does not seem to have any properties. How do you want the conversion to work out?

Here is a partial example:
public IList<Document> GetDocumentsByAccessGroup(Guid accessGroupGuid, string isActive)
{
    try
    {
        IList<ReportListAccessGroupDetailsInfo> list = DocumentProvider.GetDocumentsByAccessGroup(accessGroupGuid, isActive);
        List<Document> result = new List<Document>();
        
        foreach (ReportListAccessGroupDetailsInfo item in list)
        {
            Document d = new Document();
            
            // Convert properties as required. You haven't show what properties Document has, so
            //  I don't know what to show you
            
            result.Add(d);
        }
        
        return result;
    }
    catch (Exception ex)
    {                
        throw new Exception(string.Format("An exception occurred retrieving the list of documents", ex.ToString()));
    }

    return null;
}

Open in new window

Author

Commented:
Thanks to both of you...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial