0

List オブジェクトを使用してユーザー プロパティを格納しています。

私のコードは次のとおりです。

 string department=string.Empty;
 List<string> otherDepartments = new List<string>();
 int no;
 string result = string.Empty;

 string queryText = string.Empty;
 using (SPSite siteCollection = SPContext.Current.Site)
 {
     using(SPWeb site = siteCollection.OpenWeb())
     {
         SPSecurity.RunWithElevatedPrivileges(delegate()
         {
              SPUser spUser = site.CurrentUser;                        
              SPServiceContext context = SPServiceContext.GetContext(siteCollection);
              UserProfileManager upm = new UserProfileManager(context);
              UserProfile profile = upm.GetUserProfile(spUser.LoginName);
              department = profile["oiplbDepartment"].Value.ToString();

              UserProfileValueCollection otherDept = profile["oiplbOtherDepartments"];

              if (otherDept.Count != 0)
              {                           
                  foreach (var prop in otherDept)
                  {
                      otherDepartments.Add(prop.ToString());
                  }
               }
               Label1.Text = department + " Ohter Departments " + otherDepartments;
          });

          SPList list = site.Lists["Events"];
          SPListItemCollection itemColl;
          SPQuery query = new SPQuery();
          no = 1 + otherDepartments.Count;
          for (int i = 1; i <= no; i++)
          {
              if (no == 1)
              {
                  result = "<or> <Eq><FieldRef Name='oiplbDepartment' /><Value Type='TaxonomyFieldType'>"+department+"</Value></Eq>"+"</or>";
                  break;
              }
              else if (i == 2)
              {
                   result = "<or> <Eq><FieldRef Name='oiplbDepartment' /><Value Type='TaxonomyFieldType'>" + department + "</Value></Eq>" + 
                   "<Eq><FieldRef Name='oiplbDepartment' /><Value Type='TaxonomyFieldType'>" + otherDepartments[i-1] + "</Value></Eq>";
              }
              else if(i >= 3)
              {
                  result = generateOr(result,otherDepartments[i-1]);
              }
         }                                  
         queryText = "<where>"+result +"</Where>";
         query.Query = queryText;
         itemColl = list.GetItems(query);
         Grid.DataSource = itemColl.GetDataTable();
         Grid.DataBind();
    }
}

public static string generateOr(string temp, string value)
{
    string r = "<or>" + temp + " <Eq><FieldRef Name='oiplbDepartment' /><Value Type='TaxonomyFieldType'>" + value + "</Value></Eq> </or>";
    return r;
}

プログラムを実行すると、上記のエラーが発生します。

プロパティが利用可能な場合にのみ値を挿入し、そうでない場合は挿入します。

しかし、なぜエラーが発生するのですか?

4

1 に答える 1

4

そのせいで

no = 1 + otherDepartments.Count;

に変更します

no = otherDepartments.Count;

iリスト内の項目にアクセスする前にから 1 を引いても、for-loopまでループしますi == noi <= noしたがって、次のように変更することもできますi < no

for (int i = 1; i < no; i++)
于 2013-09-30T11:16:43.113 に答える