SharePoint、List.Items和List.GetItems(查询)和Linq

SharePoint、List.Items和List.GetItems(查询)和Linq,linq,sharepoint,sharepoint-2007,list,Linq,Sharepoint,Sharepoint 2007,List,根据建议,我尝试使用List.GetItems(查询)检索初始数据子集,而不是通过List.Items检索整个列表内容。但是,List.Items.Cast()会为Linq生成可用的IEnumerable,而List.GetItems(Query).Cast()则不会 工作代码: IEnumerable<SPListItem> results = SPContext.Current.Web.Lists[ListName].Items.Cast<SPListItem>().

根据建议,我尝试使用List.GetItems(查询)检索初始数据子集,而不是通过List.Items检索整个列表内容。但是,List.Items.Cast()会为Linq生成可用的IEnumerable,而List.GetItems(Query).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();
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();
IEnumerable results=SPContext.Current.Web.Lists[ListName].Items.Cast().Where(item=>item[“Date”!=null).Where(item=>DateTime.Parse(item[“Date”].ToString())>=StartDate).Where(item=>DateTime.Parse(item[“Date”].ToString())
System.Runtime.InteropServices.COMException
(0x80004005):无法完成此操作
操作。请重试。位于
Microsoft.SharePoint.Library.SPRequestInternalClass.GetListItemDataWithCallback(字符串
bstrUrl,字符串bstrListName,字符串
bstrViewName,字符串bstrViewXml,
安全阵列标记FSAFarrayFlags,
ISP2DSafeArrayWriter回调,
ISPDataCallback和ISPDataCallback,
ISPDataCallback(pSchemaCallback)位于
Microsoft.SharePoint.Library.SPRequest.GetListItemDataWithCallback(字符串
bstrUrl,字符串bstrListName,字符串
bstrViewName,字符串bstrViewXml,
安全阵列标记FSAFarrayFlags,
ISP2DSafeArrayWriter回调,
ISPDataCallback和ISPDataCallback,
ISPDataCallback(pSchemaCallback)---
内部异常堆栈跟踪的结束---
在
Microsoft.SharePoint.Library.SPRequest.GetListItemDataWithCallback(字符串
bstrUrl,字符串bstrListName,字符串
bstrViewName,字符串bstrViewXml,
安全阵列标记FSAFarrayFlags,
ISP2DSafeArrayWriter回调,
ISPDataCallback和ISPDataCallback,
ISPDataCallback(pSchemaCallback)位于
Microsoft.SharePoint.SPListItemCollection.EnsureListItemsData()
在
Microsoft.SharePoint.SPListItemCollection.Underty()
在
Microsoft.SharePoint.SPBaseCollection.System.Collections.IEnumerable.GetEnumerator()
在
System.Linq.Enumerable.d_uaa
1.MoveNext()
在
System.Linq.Enumerable.Count[TSource](IEnumerable
1 来源)在 Test.GetTransactionsInPeriod()位于 Test.CreateChildControls()


有人能提出什么建议吗?

从错误消息看,CAML查询似乎是错误的。您可能希望通过类似于双重检查的方式运行它。错误消息是SharePoint在请求的强制转换之前抛出的。浏览一下它,我想您有一个额外的

如果您使用Linq查询SPListItems,您应该看一看t谢谢,我将在未来的项目中研究这一点,但这一点有点晚了:(虽然已经有人回答了这个问题,但这里有一个指向U2U CAML builder的工作链接:编辑:刚刚发现这是一个功能,它通过CAML builder增强了SharePoint 2010中的功能区。好吧,你难道不知道吗?这是CAML查询。你是如何从错误消息中得出这个结论的,我不会把它放在一起的?感谢您对查询生成器的建议,它看起来是一个非常有用的工具。另外,感谢您在SPWeb.Lists[]上的指针-如果它适用于List.Items,那么它当然适用于Web.Lists!@Moo:CAML查询通常很难正确。Michael建议的U2U CAML查询生成器将为您节省大量时间。体验:-)有两件事:“无法完成此操作”。典型的Sharepoint消息是“CAML有问题”-另一个典型消息是“一个或多个字段类型未正确安装”,这也没有什么帮助,但显然是SharePoint。然后:LINQ是一种.net Framework技术,所以它应该抛出一条有意义的错误消息,所以它必须是SharePoint。通常情况下:有意义的错误=.net Framework相关,无用的错误消息=SharePoint相关。此外,它是一个SPException这一事实意味着它必须来自SharePoint的o它必须来自一个“内部”调用,只剩下两个选项:对List[ListName]的调用或对GetItems的调用。强制转换不会抛出SPException,results.Count或results.Count.ToString()也不会抛出