Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 索引超出范围。必须为非负数且小于列表中集合的大小<;字符串>;_C#_String_List_Indexoutofboundsexception - Fatal编程技术网

C# 索引超出范围。必须为非负数且小于列表中集合的大小<;字符串>;

C# 索引超出范围。必须为非负数且小于列表中集合的大小<;字符串>;,c#,string,list,indexoutofboundsexception,C#,String,List,Indexoutofboundsexception,我正在使用列表对象存储用户属性 我的代码是: 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

我正在使用列表对象存储用户属性

我的代码是:

 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;
}
string department=string.Empty;
列出其他部门=新建列表();
国际贸易编号;
字符串结果=string.Empty;
string queryText=string.Empty;
使用(SPSite siteCollection=SPContext.Current.Site)
{
使用(SPWeb site=sitecolection.OpenWeb())
{
SPSecurity.runWithLevelatedPrivileges(委托()
{
SPUser SPUser=site.CurrentUser;
SPServiceContext context=SPServiceContext.GetContext(siteCollection);
UserProfileManager upm=新的UserProfileManager(上下文);
UserProfile profile=upm.GetUserProfile(spUser.LoginName);
department=profile[“oiplbDepartment”].Value.ToString();
UserProfileValueCollection otherDept=配置文件[“OIPLDepartments”];
如果(其他部门计数!=0)
{                           
foreach(其他部门中的var prop)
{
添加(prop.ToString());
}
}
标签1.Text=部门+其他部门+其他部门;
});
SPList list=site.list[“事件”];
SPListItemCollection itemColl;
SPQuery query=新建SPQuery();
否=1+其他部门。计数;
对于(int i=1;i=3)
{
结果=生成或(结果,其他部门[i-1]);
}
}                                  
queryText=”“+结果+”;
query.query=queryText;
itemColl=list.GetItems(查询);
Grid.DataSource=itemColl.GetDataTable();
Grid.DataBind();
}
}
公共静态字符串生成器(字符串临时值、字符串值)
{
字符串r=”“+temp+“”+value+“”;
返回r;
}
我在运行程序时遇到上述错误

当且仅当属性可用,否则不可用时,我插入一个值

但是为什么我会出错呢?

这是因为

no = 1 + otherDepartments.Count;
换成

no = otherDepartments.Count;
即使在访问列表中的项目之前从
i
中减去1,您的
for loop
也会循环,直到
i==no
。因此,您也可以更改
iIts,因为

no = 1 + otherDepartments.Count;
换成

no = otherDepartments.Count;

即使在访问列表中的项目之前从
i
中减去1,您的
for loop
也会循环,直到
i==no
。因此,您也可以更改
i尝试更改此行:
for(int i=1;i)no必须至少包含值1,因此如果我更改for循环,如
for(int i=1,k=0;i尝试更改此行:
for(int i=1;i),no必须至少包含值1,因此如果我更改for循环,如
for(inti=1,k=0;我谢谢你,先生,但有一个问题是,这个数字必须至少有值1,那么我该怎么办?这是因为一个用户将至少属于一个部门。谢谢,先生,但有一个问题是,这个数字必须至少有值1,那么我该怎么办?这是因为一个用户将至少属于一个部门。