Following the recommendations, I am trying to use List.GetItems (Query) to retrieve my original subset of data, and not the entire contents of a list through List.Items. However, while List.Items.Cast () leads to a useful IEnumerable for Linq, List.GetItems (Query) .Cast () does not.
Work code:
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();
Inoperative code:
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();
The List.GetItems (Query) .Cast () method throws the following exception on the .Count () line:
Microsoft.SharePoint.SPException: This action cannot be completed. please try again. ---> System.Runtime.InteropServices.COMException (0x80004005): This action cannot be completed. Please try again. in Microsoft.SharePoint.Library.SPRequestInternalClass.GetListItemDataWithCallback (String bstrUrl, String bstrListName, String bstrViewName, String bstrViewXml, SAFEARRAYFLAGS fSafeArrayFlags, ISP2DSafeArrayWriter pSACallback, ISPDataCallback pPagingCallback, ISPDataCallback pSchemaCallback) at Microsoft.SharePoint.Library.SPRequest.GetListItemDataWithCallback (String bstrUrl, String bstrListName, String bstrViewName, String bstrViewXml, SAFEARRAYFLAGS fSafeArrayFlags, ISP2DSafeArrayWriter pSACallback, ISPDataCallback pPagingCallback, ISPDataCallback pSchemaCallback) --- End of inner exception stack trace --- in Microsoft.SharePoint.Library.SPRequest.GetListItemDataWithCallback (String bstrUrl, String bstrListName, String bstrViewName , String bstrViewXml, SAFEARRAYFLAGS fSafeArrayFlags, ISP2DSafeArrayWriter pSACallback, ISPDataCallback pPagingCallback, ISPDataCallback pSchemaCallback) with Microsoft.SharePoint.SPListItemCollection .EnsureListItemsData () in Microsoft.SharePoint.SPListItemCollection.Undirty () in Microsoft.SharePoint.SPBaseCollection.System.Collections.IEnumerable.GetEnumerator () in System.Linq.Enumerable.d__aa 1.MoveNext() at System.Linq.Enumerable.Count[TSource](IEnumerable 1 source) in Test.GetTransactionsInPeriod () at Test.CreateChildControls ()
Can anyone suggest something?