开发者

LINQ Join or GroupJoin or Union?

I have a 2 lists that contain BreadCrumbItem object:

    public BreadcrumbItem()
    {
        this.Title = string.Empty;
        this.Url = string.Empty;
        this.BreadcrumbType = BreadcrumbType.None;
    }

public enum BreadcrumbType
{
    /// <summary>
    /// Used for None type.
    /// </summary>
    None,

    /// <summary>
    /// Used for Area breadcrumb.
    /// </summary>
    Area,

    /// <summary>
    /// Used for Controller breadcrumb.
    /// </summary>
    Controller,

    /// <summary>
    /// Used for Action breadcrumb.
    /// </summary>
    Action
}

I need to join these two lists However, I need to overwrite properties of List1 with properties of List2 (only to those items that have BreadcrumbType specified).

So for example:

var List1 = new List<BreadCrumbItem>()
List1.Add(new BreadcrumbItem(){ "List1Test1", "/home/", "Controller"});
List1.Add(new BreadcrumbItem(){ "List1Test2", "/view/", "Action"});
List1.Add(new BreadcrumbItem(){ "List1Test2", "/vi开发者_Python百科ew/", null});

var List2 = new List<BreadCrumbItem>()
List2.Add(new BreadcrumbItem(){ "List2Test1", "/test/", "Controller"});
List2.Add(new BreadcrumbItem(){ "List2Test2", "/test3/", null});
List2.Add(new BreadcrumbItem(){ "List2Test3", "/test3/", null});

after the join I'm suppose to have a list that has the following items:

List2Test1
List1Test2
List1Test2
List2Test2
List2Test3

Order is important.

Please help. thanks


class Program {
    static void Main(string[] args) {

        var List1 = new List<BreadcrumbItem>();
        List1.Add(new BreadcrumbItem("List1Test1", "/home/", BreadcrumbItem.BreadcrumbType.Controller));
        List1.Add(new BreadcrumbItem("List1Test2", "/view/", BreadcrumbItem.BreadcrumbType.Action));
        List1.Add(new BreadcrumbItem("List1Test2", "/view/", BreadcrumbItem.BreadcrumbType.None));

        var List2 = new List<BreadcrumbItem>();
        List2.Add(new BreadcrumbItem("List2Test1", "/test/", BreadcrumbItem.BreadcrumbType.Controller));
        List2.Add(new BreadcrumbItem("List2Test2", "/test3/", BreadcrumbItem.BreadcrumbType.None));
        List2.Add(new BreadcrumbItem("List2Test3", "/test3/", BreadcrumbItem.BreadcrumbType.None));

        List<BreadcrumbItem> combined = List1.Union(List2).ToList<BreadcrumbItem>();
        List<BreadcrumbItem> ordered = (combined.OrderBy(a => a.orderTitle)).ToList<BreadcrumbItem>();

        foreach (var item in ordered) {
            Console.WriteLine(((BreadcrumbItem)item).Title);
        }

        Console.ReadLine();
    }

    class BreadcrumbItem {


        public string Title;
        public string Url;
        public BreadcrumbType breadcrumbType;
        public BreadcrumbItem() {
            this.Title = string.Empty;
            this.Url = string.Empty;
            this.breadcrumbType = BreadcrumbType.None;
        }
        public BreadcrumbItem(string title, string url, BreadcrumbType type) {
            this.Title = title;
            this.Url = url;
            this.breadcrumbType = type;
        }

        public string orderTitle {
            get {
                string dummy;
                dummy = Title.Substring(Title.IndexOf("Test"));
                dummy += Title.Substring(0,Title.IndexOf("Test"));
                return dummy;
            }

        }

        public enum BreadcrumbType {
            /// <summary>
            /// Used for None type.
            /// </summary>
            None,

            /// <summary>
            /// Used for Area breadcrumb.
            /// </summary>
            Area,

            /// <summary>
            /// Used for Controller breadcrumb.
            /// </summary>
            Controller,

            /// <summary>
            /// Used for Action breadcrumb.
            /// </summary>
            Action
        }
    }
}

I modified the code a bit and added orderTitle. But the result should be correct.


I ended up not using LINQ:

            this.Items = new List<BreadcrumbItem>();
            foreach (var item in virtualPathData)
            {
                if (customBreadcrumbs != null)
                {
                    var replaceItem =
                        customBreadcrumbs.Items.FirstOrDefault(t => t.BreadcrumbType == item.BreadcrumbType);
                    if (replaceItem != null)
                    {
                        this.Items.Add(replaceItem);
                        customBreadcrumbs.Items.Remove(replaceItem);
                        continue;
                    }
                }

                this.Items.Add(item);                                        
            }
            if (customBreadcrumbs != null)
            {
                foreach (var breadcrumb in customBreadcrumbs.Items)
                {
                    this.Items.Add(
                        breadcrumb.Url.IndexOf("?") == 0
                            ? new BreadcrumbItem(
                                  breadcrumb.Title,
                                  string.Concat(this.Items.Last().Url, breadcrumb.Url),
                                  BreadcrumbType.None)
                            : breadcrumb);
                }

            }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜