【问题标题】:Lists and Values of the list in C#C#中列表的列表和值
【发布时间】:2013-06-04 19:10:19
【问题描述】:

我来自 PHP,我认为我不了解如何正确遍历数据结构。 SPFieldCollection.Add 的文档非常清楚它需要什么样的参数,所以我写了一个类:

namespace SPAPI.Lists.Add
{
    class AddParams
    {
        public SPFieldType type { get; set; }
        public bool required { get; set; }
    }
}

从那里我创建了一个列表:

public Create(SPFeatureReceiverProperties properties, Dictionary<string, List<AddParams>> columns, string name, string description, SPListTemplateType type)
{
    SPSite siteCollection = properties.Feature.Parent as SPSite;
    if (siteCollection != null)
    {
        SPWeb web = siteCollection.RootWeb;
        web.Lists.Add(name, description, type);
        web.Update();

        // Add the new list and the new content.
        SPList spList = web.Lists[name];
        foreach(KeyValuePair<string, List<AddParams>> col in columns){
            spList.Fields.Add(col.Key, col.Value[0], col.Value[1]);
        }

        // More Code ...
    }
}

问题是,它不喜欢col.Value[0]col.Value[1],因为按照严格的定义,一个不是SPFieldType,另一个不是boolean。我认为我的想法是正确的,但我正在寻找有关如何完成这项工作的指导。

因为 C# 有类型提示,我假设它会使用 AddParams 类来查看类型。

这个想法是传递一系列参数并基于这些参数创建一个新列表。

这更像是一个 C# 问题和数据结构迭代问题,然后是一个 SP 开发问题。

【问题讨论】:

  • 您是否尝试将 col.Value[0] 和 col.Value[1] 转换为各自的类型?
  • @Rubixus Cast what...那些都是 AddParams,只是第一个和第二个。
  • @Don 也许我不明白你的问题。对我来说,您的问题似乎是 col.Value[0] 应该作为 SPFieldType 添加到 Fields,而 col.Value[1] 应该是布尔值。但在看到伊戈尔的回答后,我明白你在追求什么。

标签: c# list data-structures for-loop sharepoint-deployment


【解决方案1】:

col.Value[0]col.Value[1] 都是 AddParams 类型。这可能会编译:

spList.Fields.Add(col.Key, col.Value[0].type, col.Value[0].required);

但您可能需要在您的 foreach 中添加另一个 foreach

foreach(AddParams item in col.Value)
{
}

【讨论】:

    【解决方案2】:

    改变

    spList.Fields.Add(col.Key, col.Value[0], col.Value[1]);
    

    spList.Fields.Add(col.Key, col.Value[0].type, col.Value[0].required);
    

    【讨论】:

      【解决方案3】:

      为什么你有AddParams 的列表?您是否期望同一字段超过 1 个FieldType

      我认为你应该这样实现它:

      public Create(SPFeatureReceiverProperties properties, Dictionary<string, AddParams> columns, string name, string description, SPListTemplateType type)
      {
          SPSite siteCollection = properties.Feature.Parent as SPSite;
          if (siteCollection != null)
          {
              SPWeb web = siteCollection.RootWeb;
              web.Lists.Add(name, description, type);
              web.Update();
      
              // Add the new list and the new content.
              SPList spList = web.Lists[name];
              foreach(string key in columns.Keys){
                  spList.Fields.Add(key, columns[key].type, columns[key].required);
              }
      
              // More Code ...
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-28
        • 1970-01-01
        • 2017-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多