Linq: Sorting a Splitting a 1 level list to a 2 level list

Nice to have.

This sample converts the 1 Level list of ProjectLink’s to a sorted 2 Level list of BizArea’s

        void populate()
            var projList = new List<ProjectLink>();

            //Save to list to enable linq, but also to compute column BizArea from Url
            var proj = new ProjectLink() {DisplayName = "My Project", Url = "http://domain/ba1/sitename1"};

            //Convert 1 level project list to a 2 level ba list
            var sortedBas = Create2LevelList(projList);

        /// <summary>
        /// Convert 1 level project list to a 2 level ba list
        /// </summary>
        /// <param name="projList"></param>
        /// <returns></returns>
        private static List<BizArea> Create2LevelList(List<ProjectLink> projList)
            //Save to sorted list on DisplayName
            var sortedRowsQ = from o in projList
                              orderby o.DisplayName
                              select o;
            //var sortedRowsQ = projList.OrderBy(o => o.DisplayName, StringComparer.InvariantCultureIgnoreCase);
            var sortedRows = sortedRowsQ.ToList();

            //Save to sorted list of BA's and create a ProjectList, where the projects can be added to
            var sortedBasQ = from o in projList
                             group o by o.BizArea
                             into ba
                             orderby ba.Key
                             select new BizArea() {BaName = ba.Key, BaProjects = new List<ProjectLink>()};
            List<BizArea> sortedBas = sortedBasQ.ToList();

            //add projects to ba lists
            foreach (var sortedBa in sortedBas)
                //Create a ProjectList for each ba and save the link to the new ProjectList in the sortedBa

                //add projects to ba lists
                foreach (var projectLink in sortedRows)
                    //if this project belongs to same BA as the current BA, then add it to the projectlist for this BA
                    if (projectLink.BizArea == sortedBa.BaName)
            return sortedBas;

List Entities

    public class ProjectLink
        private readonly char[] _charArr = {'/'};

        public string DisplayName { get; set; }
        public string BizArea { get; private set; }

        private string _url;
        public string Url
            get { return _url; }
                _url = value;

                //Set also BizArea
                var urlarr = Url.Split(_charArr);
                if (urlarr.Length > 3)
                    //Url = "http://domain/ba/sitename" - Ba is 4th elem
                    BizArea = urlarr[3];

    public class BizArea
        public string BaName { get; set; }
        public List<ProjectLink> BaProjects { get; set; }

The End


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: