Caml multi-table associated with the query

the using (the SPSite the SPSite Site new new = (the SiteUrl)) 
{ 
    the using (= site.RootWeb the SPWeb Web) 
    { 
        the SPQuery the SPQuery new new Query = (); 
        // the Joins property, there are two ways to connect INNER and LEFT, can query, and It supports multi-table joins; 
        query.Joins = "<the Join the Type = 'the INNER' ListAlias = 'City'>" + 
                            "<Eq>" + 
                                "<the FieldRef the Name = 'the Location' RefType = 'Id' />" + 
                                "< List = the FieldRef 'City' the Name = 'ID' /> "+ 
                            " </ Eq> "+ 
                        " </ the Join> "; 
        // Now setting items associated with the field 
        query.ProjectedFields =" <Field Name='CustomCityID' Type='Lookup' " + 
                                        "List = 'City' ShowField = 'CityID' />";
<Field Name='CustomMark' Type='Lookup' List='City' ShowField='Mark'/>";
        //设置需要显示的字段
        query.ViewFields = "<FieldRef Name='Title'/>" +
                            "<FieldRef Name='Location'/>" +
                            "<FieldRef Name='CustomCityID'/>";
"<FieldRef Name='CustomMark'/>";

        SPList list = web.GetList(ListName);
        SPListItemCollection itemcoll = list.GetItems(query);
        foreach (SPListItem item in itemcoll)
        {
            SPFieldLookupValue Location = new SPFieldLookupValue(item["Location"].ToString());
            = New new SPFieldLookupValue CustomCityID SPFieldLookupValue (Item [ "CustomCityID"] the ToString ().); 
            Console.WriteLine ( "ID:." Item.ID.ToString + () + "the Title:" Item + [ "the Title"] the ToString () + "the Location:" + + Location.LookupValue "CustomCityID:" + CustomCityID.LookupValue); 
        } 
    } 
} 

. 9, at the same time add more code ProjectedFields Mark field properties, as shown below: 

query.ProjectedFields = 
"<field, the Name = ' CustomCityID 'the Type =' the Lookup 'List =' City 'the ShowField =' cityId '/> 
<Field, the Name =' CustomMark 'the Type =' the Lookup 'List =' City 'the ShowField =' Mark '/> "; 
  10, while the display CustomMark added attribute field, as follows: 

query.ViewFields = "<the FieldRef the Name = 'Title'/>" +
                    "<FieldRef Name='Location'/>" +
                    "<The FieldRef the Name = 'CustomCityID' />" + 
                    "<the FieldRef the Name = 'CustomMark' />"; 
  add the output value CustomMark field as follows:

= New new SPFieldLookupValue CustomMark SPFieldLookupValue (Item [ "CustomMark"] the ToString ().); 
Console.WriteLine ( "CustomMark:" + CustomMark.LookupValue) 

attached: list structure 

  list a: City 

  field: the Title (the default field, single line of text, as Address associated with the inspection items) 

     cityId (digital type, as Address associated inspection items) 

     Mark (single line of text, as a multi-table query test field) 

  a list of two: Address 

  field: Title (default) 

     the Location (SEE items associated with City list the Title) 

     CustomCityID (SEE items associated with City list cityID) 

attachment: reference links 

  http://social.msdn.microsoft.com/Forums/sharepoint/zh-CN/866a49fd-0656-4a8a-9e11-36eeb4401005/sharepoint-2010 List-joins--caml 

  http://msdn.microsoft.com/zh-cn/library/microsoft.sharepoint.spquery.joins.aspx

  

Guess you like

Origin www.cnblogs.com/Nigel/p/11237124.html