当前位置:  开发笔记 > 编程语言 > 正文

使用lambdas选择子对象集合

如何解决《使用lambdas选择子对象集合》经验,为你挑选了1个好方法。

我有以下类对象:

    public class VacancyCategory
{
    public int ID { get; set; }
    public string Text { get; set; }
    public IList SubCategories { get; set; }
}

    public class VacancySubCategory
{
    public int ID { get; set; }
    public string Text { get; set; }
    public VacancyCategory Category { get; set; }
    public IList Vacancies { get; set; }
}

    public class Vacancy : IBusinessObject
{
    public int ID { get; set; }
    public string Title { get; set; }
    public VacancySubCategory SubCategory { get; set; }
    public string Body { get; set; }
    public VacancyWorkType WorkType { get; set; }
    public string Salary { get; set; }
    public DateTime? AppsClosingDate { get; set; }
    public bool Active { get; set; }
}

...所以在测试库中创建测试数据,如下所示:

        private IList GetVacancyCategoriesWithAllChildCollections()
    {
        IList vacancyCategories = new List();

        int cCounter = 0;
        int scCounter = 0;
        int vCounter = 0;
        for (int i = 1; i <= 3; i++)
        {
            VacancyCategory vc = new VacancyCategory();
            vc.ID = ++cCounter;
            vc.Text = "VacancyCategory" + i.ToString();

            for (int j = 1; j <= 3; j++)
            {
                VacancySubCategory vsc = new VacancySubCategory();
                vsc.ID = ++scCounter;
                vsc.Text = "VacancySubCategory" + scCounter.ToString();
                vsc.Category = vc;

                for (int k = 1; k <= 2; k++)
                {
                    Vacancy v = new Vacancy();
                    v.ID = ++vCounter;
                    v.Title = "Vacancy" + vCounter.ToString();
                    v.Body = "VacancyBody" + vCounter.ToString();
                    v.Active = vCounter >= 16 ? false : true;
                    v.WorkType = this._workTypes.Single(wt => wt.ID == k);
                    v.Salary = vCounter <= 7 ? "SR " + (vCounter * 1000).ToString() : "";
                    v.AppsClosingDate = (vCounter >= 3 & vCounter <= 13) ? (new DateTime(2009, 3, vCounter)) : (DateTime?)null;
                    v.SubCategory = vsc;

                    if (vsc.Vacancies == null)
                        vsc.Vacancies = new List();
                    vsc.Vacancies.Add(v);
                }

                if (vc.SubCategories == null)
                    vc.SubCategories = new List();
                vc.SubCategories.Add(vsc);
            }

            vacancyCategories.Add(vc);
        }

        return vacancyCategories;
    }

..所以现在我有一些很好的测试数据.对象树/链式对象对我很重要.

所以我想在需要时从这个树中返回单个对象集合.例如,如果我想要整个树,我可以返回包含所有子对象的VacancyCategory列表 - 很棒.但现在我想只返回VacancySubCaregory项目(全部9个).这将是我对测试库的公共方法:

        public IQueryable GetVacancySubCategories()
    {

         throw new NotImplementedException("write gen code");
    }

..显然没有例外.我有一个名为_categories的成员字段,其中包含GetVacancyCategoriesWithAllChildCollections方法的结果.所以我一直在尝试像

this._categories.Select( ......

..但我似乎无法返回VacancySubCategory对象的列表.我似乎总是选择根集合(即VacancyCategory对象的结果集).我究竟做错了什么?我确定它很简单...但它让我疯了!

编辑

thanx亚光.

你的建议让我这样:

        public IQueryable GetVacancySubCategories()
    {
        return this._categories.SelectMany(c => c.SubCategories).AsQueryable();
    }

..哪个很棒.你是冠军



1> Matt Hamilto..:

尝试:

return this._categories.SelectMany(c => c.SubCategories);

推荐阅读
jerry613
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有