开发者

Process taking lots of time in foreach loop in C#

I am using C# code and Tridion(CMS) classes to fetch data from Tridion, below is the code to get all the publications List from 开发者_如何学PythonTridion.

protected void btnPublishPublicationList_Click(object sender, EventArgs e)
    {
        try
        {
            PublicationBL pubBL = new PublicationBL();
            TridionCollection<Publication> pubAllList = pubBL.getAllPublicationList();
            List<PublicationsBO> pubBos = new List<PublicationsBO>();

            foreach (Publication pub in pubAllList)
            {
                if ((pub.Title.StartsWith("07"))||(pub.Title.StartsWith("08")))
                {
                    PublicationsBO pubBO = new PublicationsBO();
                    pubBO.publicationID = pub.ID;
                    pubBO.publicationName = pub.Title;
                    pubBos.Add(pubBO);
                }
            }

            pubBL.createPublicationListXML(pubBos);          
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }
    }

In above code on the button click, I am using .net code and using Tridion class to get all the publications List as below:

TridionCollection<Publication> pubAllList = pubBL.getAllPublicationList();

I am getting my all the publications list very fast from the Tridion, however when I am going for foreach loop as below my process gets stuck and it takes lots of time to do this.

foreach (Publication pub in pubAllList)
            {
                if ((pub.Title.StartsWith("07"))||(pub.Title.StartsWith("08")))
                {
                    PublicationsBO pubBO = new PublicationsBO();
                    pubBO.publicationID = pub.ID;
                    pubBO.publicationName = pub.Title;
                    pubBos.Add(pubBO);
                }
            }

After debugging I found that when debugger comes to foreach (Publication pub in pubAllList) it is taking lots of time. I think while making the Publication class object is taking time and it is Tridion class.

Please suggest any other way to do this or suggest what is wrong in above code.

Thanks.


It is indeed because of Tridion's lazy-loading. If all you need is a list of Publication IDs and Titles, I'd recommend using:

TDSE tdse = new TDSEClass():
XmlDocument publicationList = new XmlDocument();
publicationList.LoadXml(tdse.GetListPublications(ListColumnFilter.XMLListIDAndTitle));

This will give you an XML document containing a list of all publications (/tcm:ListPublications/tcm:Item), and each Item will contain a Title and ID attributes.

If you need more detail than just ID and Title, then you will have to individually load each publication, which you can do by using the ID attribute tdse.GetObject().

Hope this helps. N


according to this which I think is the developers site... it looks like getAllPublicationList is using some sort of lazy loading so even though you have the collection you don't really have the items in it.

It appears that you can set filters on their collection rather than after the fact so that it will only load the records you are interested in.

to clarify, lazy loading means that when the collection is returned the data needed to populate the items in it are not yet laded. It isn't until you access the item in the collection that the data (e.g. by creating a Publication item) for that item is actually loaded. The purpose for using a lazy collection is to allow filtering on the collection so that unnecessary expensive loads can be avoided.

HTH

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜