Moo*_*Moo 4 linq sharepoint list sharepoint-2007
继建议之后,我试图使用List.GetItems(Query)来检索我的初始数据子集,而不是通过List.Items检索整个列表内容.但是,List.Items.Cast()导致Linq可用的IEnumerable,List.GetItems(查询).Cast()不会.
工作守则:
IEnumerable<SPListItem> results = SPContext.Current.Web.Lists[ListName].Items.Cast<SPListItem>().Where(item => item["Date"] != null).Where(item => DateTime.Parse(item["Date"].ToString()) >= StartDate).Where(item => DateTime.Parse(item["Date"].ToString()) <= EndDate);
MessageLine = results.Count().ToString();
Run Code Online (Sandbox Code Playgroud)
非工作代码:
string SPStartDate = SPUtility.CreateISO8601DateTimeFromSystemDateTime(this.StartDate);
string SPEndDate = SPUtility.CreateISO8601DateTimeFromSystemDateTime(this.EndDate);
SPQuery MyQuery = new SPQuery();
MyQuery.Query = "<Where><And><And><Geq><FieldRef Name='Date'/><Value Type='DateTime'>" + SPStartDate + "</Value></Geq><Leq><FieldRef Name='Date'/><Value Type='DateTime'>" + SPEndDate + "</Value></Leq></And></Where>";
IEnumerable<SPListItem> results = SPContext.Current.Web.Lists[ListName].GetItems(MyQuery).Cast<SPListItem>();
MessageLine = results.Count().ToString();
Run Code Online (Sandbox Code Playgroud)
List.GetItems(Query).Cast()方法在.Count()行上产生以下异常:
Microsoft.SharePoint.SPException:无法完成此操作.请再试一次.---> System.Runtime.InteropServices.COMException(0x80004005):无法完成此操作.请再试一次.在Microsoft.SharePoint.Library.SPRequestInternalClass.GetListItemDataWithCallback(字符串bstrUrl,字符串bstrListName,字符串bstrViewName,字符串bstrViewXml,SAFEARRAYFLAGS fSafeArrayFlags,ISP2DSafeArrayWriter pSACallback,ISPDataCallback pPagingCallback,ISPDataCallback pSchemaCallback)在Microsoft.SharePoint.Library.SPRequest.GetListItemDataWithCallback(字符串bstrUrl,字符串bstrListName,字符串bstrViewName,字符串bstrViewXml,SAFEARRAYFLAGS fSafeArrayFlags,ISP2DSafeArrayWriter pSACallback,ISPDataCallback pPagingCallback,ISPDataCallback pSchemaCallback)---内部异常堆栈跟踪的末尾在Microsoft.SharePoint.Library.SPRequest.GetListItemDataWithCallback(字符串bstrUrl,字符串bstrListName,字符串Microsoft.SharePoint.SPListItemCol上的Microsoft.SharePoint.SPListItemCollection.EnsureListItemsData()中的bstrViewName,String bstrViewXml,SAFEARRAYFLAGS fSafeArrayFlags,ISP2DSafeArrayWriter pSACallback,ISPDataCallback pPagingCallback,ISPDataCallback pSchemaCallback)
1.MoveNext() at System.Linq.Enumerable.Count[TSource](IEnumerable在Test.CreateChildControls()的Test.GetTransactionsInPeriod()中的System.Linq.Enumerable.d__aa 1源码处的Microsoft.SharePoint.SPBaseCollection.System.Collections.IEnumerable.GetEnumerator()处的lection.Undirty()
谁能提出任何建议?
Mic*_*tum 15
从错误消息看起来CAML查询是错误的.您可能希望通过U2U的CAML查询生成器等操作来进行双重检查.SharePoint在请求的强制转换之前抛出错误消息.瞥了一眼,我觉得你<And>在开头有一个额外的(<Where><And><And>)
顺便说一下:不要使用SPWeb.Lists [Name].这将加载SPWeb中的每个列表(包括元数据),这是相当大的资源.其中一个SPWeb.GetList或SPWeb.Lists.GetList方法更好.