考虑到对lookup字段的CAML查询的模糊,在此举例验证。
SPQuery query = new SPQuery(); query.Query = @"<Where> <Gt> <FieldRef Name=‘Salary‘/> <Value Type=‘Text‘>2000</Value> </Gt> </Where>";
注意:该查询语句返回的是薪资大于2000的薪资信息。
query.Joins = @"<Join Type=‘LEFT‘ ListAlias=‘Employee‘> <Eq> <FieldRef Name=‘Employee‘ RefType=‘Id‘/> <FieldRef List=‘Employee‘ Name=‘Id‘/> </Eq> </Join>";
注意:<FieldRef Name=‘Employee‘ RefType=‘Id‘/>指定薪资列表lookup字段名称,RefType永远是Id; <FieldRef List=‘Employee‘ Name=‘Id‘/> 指定了关联列表同名词,Name永远是Id。
query.ProjectedFields = @"<Field Name=‘FullName‘ Type=‘Lookup‘ List=‘Employee‘ ShowField=‘EmployeeFullName‘/>";
query.ViewFields = @"<FieldRef Name=‘Employee‘/> <FieldRef Name=‘Salary‘/><FieldRef Name=‘FullName‘/>";
SPQuery query = new SPQuery(); query.Query = @"<Where> <Gt> <FieldRef Name=‘Salary‘/> <Value Type=‘Text‘>2000</Value> </Gt> </Where>"; query.Joins = @"<Join Type=‘LEFT‘ ListAlias=‘Employee‘> <Eq> <FieldRef Name=‘Employee‘ RefType=‘Id‘/> <FieldRef List=‘Employee‘ Name=‘Id‘/> </Eq> </Join>"; query.ProjectedFields = @"<Field Name=‘FullName‘ Type=‘Lookup‘ List=‘Employee‘ ShowField=‘EmployeeFullName‘/>"; query.ViewFields = @"<FieldRef Name=‘Employee‘/> <FieldRef Name=‘Salary‘/><FieldRef Name=‘FullName‘/>"; string str = ""; using(SPSite site = new SPSite("http://dev-sp")){ SPWeb web = site.RootWeb; SPList list = web.Lists.TryGetList("Salary"); SPListItemCollection items = list.GetItems(query); foreach(SPListItem item in items){ str += item["Employee"].ToString() + ":" + item["Salary"] + "-" + item["FullName"].ToString() + "\n"; } } Console.WriteLine(str); Console.ReadLine();
注意: 由于员工是lookup字段类型,所以会有字符出现,这里我没有做处理,需要的可以自行更改。
原文:http://blog.csdn.net/tristan_dong/article/details/18411959