我怎样才能从另一个类中填写一个类属性?

时间:2013-08-03 16:07:58

标签: c# linq

我有一个类,我有FreeDressingItems,FreeToppingItems,FreeInstructionItems

的集合

就像这样填充selectedCustomization我在这个类中有另一个属性public string Items { get { return GetAllItems(); } }

我想填充以便它保留相同类别类型的所有catetoryname,以便我可以轻松地将其绑定到网格并以逗号分隔的形式显示其所有值。

我有以下代码,有人可以帮助我,我怎么能做到这一点。

  public class selectedCustomization
    {
        public CategoryType TypeName { get; set; }
        public string CategoryName { get; set; }
        public string ItemName { get; set; }
        public int SourceID { get; set; }
        public string Items { get { return GetAllItems(); } }
        private string GetAllItems()
        {
            switch (TypeName)
            {
                    case CategoryType.Dressing:
                    {
                        cFreeCustomization cfreeCust = new cFreeCustomization();
                        break;
                     }


                case CategoryType.Topping:

                    break;

                case CategoryType.SpecialInstruction:

                    break;


            }

        }

    }

这是另一个类cFreeCustomization

public List<selectedCustomization> SelectedItems
    {
        get
        {
            libDBDataContext cn = new libDBDataContext();
            List<selectedCustomization> lst = new List<selectedCustomization>();
            lst.AddRange(
                        (from xx in this.FreeDressingItems
                         select new selectedCustomization() { TypeName = CategoryType.Dressing, CategoryName = xx.DressingInfo.CatName, ItemName = xx.DressingInfo.Description }
                        ).ToList()
                        );
            lst.AddRange(
                        (from xx in this.FreeToppingItems
                         select new selectedCustomization() { TypeName = CategoryType.Topping, CategoryName = xx.ToppingInfo.CatName, ItemName = xx.ToppingInfo.Description }
                        ).ToList()
                        );
            lst.AddRange(
                        (from xx in this.FreeInstructionItems
                         select new selectedCustomization() { TypeName = CategoryType.SpecialInstruction, CategoryName = xx.InstructionInfo.CatName, ItemName = xx.InstructionInfo.Description }
                        ).ToList()
                        );
            return lst;
        }
    }

如何以逗号分隔的形式制作selectedCustomization的组合?

1 个答案:

答案 0 :(得分:1)

我认为 GetAllItems 方法应如下所示:

private string GetAllItems()
{
    cFreeCustomization cfreeCust = new cFreeCustomization();
    var ls = cfreeCust.SelectedItems.FindAll(I => I.TypeName == this.TypeName);
    return string.Join(",", ls.Select(I => I.CategoryName).ToArray());  
}

这将解决您的问题。