Joins in SPQuery
Here am using two lists.
1)Customer
2) CustomerCity
Am displaying both lists in gridview.
here lookup list is CustomerCity.
Lookup is either ID field or Title field while creating list in customer list.
string siteUrl = "http://windows:1122/";
SPWeb _web = new SPSite(siteUrl).OpenWeb();
SPQuery _query = new SPQuery();
_query.Query = "";
_query.Joins = @"<Join Type='LEFT' ListAlias='CustomerCity'>
<Eq>
<FieldRef Name='City' RefType='ID' />
<FieldRef List='CustomerCity' Name='ID' />
</Eq>
</Join>";
_query.ProjectedFields = @"<Field Name='CityTitle' Type='Lookup' List='CustomerCity' ShowField='Title' />";
_query.ViewFields = @" <FieldRef Name='Title' />
<FieldRef Name='CityTitle' />";
_query.ViewFieldsOnly = true;
var items =_web.Lists["Customer"].GetItems(_query);
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Title"));
dt.Columns.Add(new DataColumn("CityTitle"));
foreach (SPListItem item in items)
{
dt.Rows.Add(item["Title"], item["CityTitle"]);
lblerror.Text += "---</br>" + string.Format("{0}----{1}", item["Title"], item["CityTitle"]);
}
GridView1.DataSource = dt;
GridView1.DataBind();
1)Customer
2) CustomerCity
Am displaying both lists in gridview.
here lookup list is CustomerCity.
Lookup is either ID field or Title field while creating list in customer list.
string siteUrl = "http://windows:1122/";
SPWeb _web = new SPSite(siteUrl).OpenWeb();
SPQuery _query = new SPQuery();
_query.Query = "";
_query.Joins = @"<Join Type='LEFT' ListAlias='CustomerCity'>
<Eq>
<FieldRef Name='City' RefType='ID' />
<FieldRef List='CustomerCity' Name='ID' />
</Eq>
</Join>";
_query.ProjectedFields = @"<Field Name='CityTitle' Type='Lookup' List='CustomerCity' ShowField='Title' />";
_query.ViewFields = @" <FieldRef Name='Title' />
<FieldRef Name='CityTitle' />";
_query.ViewFieldsOnly = true;
var items =_web.Lists["Customer"].GetItems(_query);
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Title"));
dt.Columns.Add(new DataColumn("CityTitle"));
foreach (SPListItem item in items)
{
dt.Rows.Add(item["Title"], item["CityTitle"]);
lblerror.Text += "---</br>" + string.Format("{0}----{1}", item["Title"], item["CityTitle"]);
}
GridView1.DataSource = dt;
GridView1.DataBind();
Comments