LINQ加入或GroupJoin还是Union?

时间:2011-09-01 13:09:07

标签: asp.net-mvc linq asp.net-mvc-3

我有一个包含BreadCrumbItem对象的2个列表:

    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
}

我需要加入这两个列表但是,我需要用List2的属性覆盖List1的属性(仅限于那些指定了BreadcrumbType的项目。)

例如:

var List1 = new List<BreadCrumbItem>()
List1.Add(new BreadcrumbItem(){ "List1Test1", "/home/", "Controller"});
List1.Add(new BreadcrumbItem(){ "List1Test2", "/view/", "Action"});
List1.Add(new BreadcrumbItem(){ "List1Test2", "/view/", 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});

在加入之后,我想有一个包含以下项目的列表:

List2Test1
List1Test2
List1Test2
List2Test2
List2Test3

订单很重要。

请帮忙。 感谢

2 个答案:

答案 0 :(得分:0)

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
        }
    }
}

我稍微修改了代码并添加了orderTitle。但结果应该是正确的。

答案 1 :(得分:0)

我最终没有使用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);
                }

            }