鄂破机看 发表于 2015-9-27 06:50:49

SharePoint列表,多条件查询

  
  下面是一个带有两个左外部联接的 Joins 元素的示例。CustomerName 是 Orders 列表上的查阅字段。它查阅 Customers 列表的 ID 字段。而 Customer 列表具有一个 CityName 字段,该字段又是 Cities 列表的查阅字段。第一个 Join 元素将"customers"指派为 Customers 列表的别名。Eq 元素的子元素定义使用构成查阅关系的相同源和目标字段的联接。第二个 Join 元素将"customerCities"指派为 Cities 列表的别名。它定义与 Customer 和 Cities 列表之间的现有查阅关系平行的联接。
  别名对列表而言非常重要,因为同一列表上可能存在多个联接,需要使用不同的别名区分这些联接。例如,除了从 Orders 到 Customer 以及从 Customer 到 Cities 的联接外,可能还存在从 Orders 到 Suppliers 以及从 Suppliers 到 Cities 的联接。在最后一种联接中,将为 Cities 列表指派一个不同于从 Customer 到 Cities 联接中所用别名的别名(如"supplierCities")。








复制




<Joins>
<Join Type=’LEFT’ ListAlias=’customers’>
<Eq>
<FieldRef Name=’CustomerName’ RefType=’Id’ />
<FieldRef List=’customers’ Name=’ID’ />
</Eq>
</Join>
<Join Type=’LEFT’ ListAlias=’customerCities’>
<Eq>
<FieldRef List=’customers’ Name=’CityName’ RefType=’Id’ />
<FieldRef List=’customerCities’ Name=’ID’ />
</Eq>
</Join>
</Joins>




using (SPSite site=new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb web=site.OpenWeb(SPContext.Current.Web.ServerRelativeUrl))
                {
                   SPQuery query = new SPQuery();
                   //Joins属性,这里有INNER和LEFT两种方式连接,均可查询,而且支持多表连接;
                   query.Joins = @"<Join Type='INNER' ListAlias='City'>" +
                                       "<Eq>" +
                                           "<FieldRef Name='Location' RefType='Id'/>" +//RefType='Id'
                                           "<FieldRef List='City' Name='ID'/>" +//ID
                                       "</Eq>" +
                                 "</Join>";
                   //设置关联的查阅项字段,多表连接支持能查询文本字段。。。
                   query.ProjectedFields = "<Field Name='CustomCityIDs' Type='Lookup' " +、//CustomCityIDs是给列重命名,避免和其他列重名;只能写成查阅项字段
                                                   "List='City' ShowField='CityID'/>"+
                                                   "<Field Name='Mark' Type='Lookup' " +//
                                                   "List='City' ShowField='Mark'/>";
                   //设置需要显示的字段
                   query.ViewFields = "<FieldRef Name='Title'/>" +
                                       "<FieldRef Name='Location'/>" +
                                       "<FieldRef Name='Mark'/>" +
                                       "<FieldRef Name='CustomCityID'/>";
                   SPList list = web.Lists.TryGetList("Address");
                   SPListItemCollection itemcoll = list.GetItems(query);
                   foreach (SPListItem item in itemcoll)
                   {
                     //SPFieldLookupValue Location = new SPFieldLookupValue(item["Location"].ToString());
                     //SPFieldLookupValue CustomCityID = new SPFieldLookupValue(item["CustomCityID"].ToString());
                     //Console.WriteLine("ID:" + item.ID.ToString() + " Title:" + item["Title"].ToString() + " Location:" + Location.LookupValue + " CustomCityID:" + CustomCityID.LookupValue);
                   }
                }
            }
            });
页: [1]
查看完整版本: SharePoint列表,多条件查询