【问题标题】:How can i fill a class prperty form another class?如何从另一个类中填充类属性?
【发布时间】:2013-08-03 16:07:57
【问题描述】:

我有课,我收集了 FreeDressingItems、FreeToppingItems、FreeInstructionItems

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

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

我有以下代码,有人可以帮我解决这个问题。

  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 关联起来?

【问题讨论】:

  • 如果您的属性在后台调用方法,只需删除该属性并创建一个用于相同目的的方法。
  • 那么我怎样才能在这里访问该集合?
  • 能否请您给我一些我辛苦了很久的想法?
  • 这里的“cFreeCustomization”类有什么用?
  • cFreeCustomization 是一个包含 SelectedItems 的单独类。

标签: c# linq


【解决方案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());  
}

这将解决您的问题。

【讨论】:

  • 但它会重新实例化整个对象,并且集合始终为空。
  • FreeDressingItems 计数 0
  • @NoviceToDotNet 请发布您的 cFreeCustomization 课程以及您如何将数据填充到 FreeDressingItems、FreeToppingItems 等中。
猜你喜欢
  • 2015-06-27
  • 2014-06-17
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 2015-10-08
相关资源
最近更新 更多