SharePoint 2010 主从表关系 主表读取从表的个数
我写的方法很普通,如果有更好的写法,请分享
针对Lookup字段的查询还是有点不同的,在下面的代码里会描述出来
先介绍两个列表,数据分组与数据字典,图片都好说明,就截图了
这个不难,就直接写代码了
using (SPSite site = new SPSite("http://moss:168")) { using (SPWeb web = site.OpenWeb()) { SPList list = web.Lists["数据分组"]; SPQuery query = new SPQuery(); query.ViewFieldsOnly = true; query.ViewFields = string.Concat( "<FieldRef Name='ID'/>", "<FieldRef Name='" + list.Fields["编码"].InternalName + "' />", "<FieldRef Name='" + list.Fields["名称"].InternalName + "'/>" ); SPListItemCollection listItemCollection = list.GetItems(query); foreach (SPListItem item in listItemCollection) { Console.WriteLine(item["ID"] + " " + item[list.Fields["编码"].InternalName] + " " + item[list.Fields["名称"].InternalName] + " " + getDictCount(web, item[list.Fields["名称"].InternalName].ToString())); } } }
下面是 getDictCount的方法
public static string getDictCount(SPWeb web, string GroupName) { SPList list = web.Lists["数据字典"]; SPQuery query = new SPQuery(); query.ViewFieldsOnly = true; query.Query = string.Concat( "<Where>", "<Eq>", "<FieldRef Name='" + list.Fields["分组"].InternalName + "'/>", "<Value Type='Lookup'>" + GroupName + "</Value>", "</Eq>", "</Where>" ); SPListItemCollection listItem = list.GetItems(query); return listItem.Count.ToString(); }
如果有写法简单的兄弟,请分享一下,呵呵