SharePoint、List.Items 和 List.GetItems(Query) 和 Linq

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1459347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:23:22  来源:igfitidea点击:

SharePoint, List.Items and List.GetItems(Query) and Linq

linqsharepointsharepoint-2007list

提问by Moo

Following on from suggestions, I am trying to use List.GetItems(Query) to retrieve my initial data subset rather than the entire list contents via List.Items. However, whereas List.Items.Cast() results in a usable IEnumerable for Linq, List.GetItems(Query).Cast() does not.

根据建议,我尝试使用 List.GetItems(Query) 来检索我的初始数据子集,而不是通过 List.Items 检索整个列表内容。但是,虽然 List.Items.Cast() 会产生一个可用于 Linq 的 IEnumerable,但 List.GetItems(Query).Cast() 不会。

Working 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();

Non-working 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 produces the following Exception on the .Count() line:

List.GetItems(Query).Cast() 方法在 .Count() 行产生以下异常:

Microsoft.SharePoint.SPException: Cannot complete this action. Please try again. ---> System.Runtime.InteropServices.COMException (0x80004005): Cannot complete this action. Please try again. at 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 --- at Microsoft.SharePoint.Library.SPRequest.GetListItemDataWithCallback(String bstrUrl, String bstrListName, String bstrViewName, String bstrViewXml, SAFEARRAYFLAGS fSafeArrayFlags, ISP2DSafeArrayWriter pSACallback, ISPDataCallback pPagingCallback, ISPDataCallback pSchemaCallback) at Microsoft.SharePoint.SPListItemCollection.EnsureListItemsData() at Microsoft.SharePoint.SPListItemCollection.Undirty() at Microsoft.SharePoint.SPBaseCollection.System.Collections.IEnumerable.GetEnumerator() at System.Linq.Enumerable.d__aa1.MoveNext() at System.Linq.Enumerable.Count[TSource](IEnumerable1 source) at Test.GetTransactionsInPeriod() at Test.CreateChildControls()

Microsoft.SharePoint.SPException:无法完成此操作。请再试一次。---> System.Runtime.InteropServices.COMException (0x80004005): 无法完成此操作。请再试一次。在 Microsoft.SharePoint.Library.SPRequestInternalClass.GetListItemDataWithCallback(String bstrUrl, String bstrListName, String bstrViewName, String bstrViewXml, SAFEARRAYFLAGS fSafeArrayFlags, ISP2DSafeArrayWriter pSACallback, ISPDataCallback pPagingCallback, bstrListName, String bstrViewName, String bstrViewXml) bstrListName、字符串 bstrViewName、字符串 bstrViewXml、SAFEARRAYFLAGS fSafeArrayFlags、ISP2DSafeArrayWriter pSACallback、ISPDataCallback pPagingCallback、1.MoveNext() at System.Linq.Enumerable.Count[TSource](IEnumerable1 个源)在 Test.GetTransactionsInPeriod() 在 Test.CreateChildControls()

Can anyone suggest anything?

任何人都可以提出任何建议吗?

回答by Michael Stum

From the error message it looks like the CAML Query is wrong. You may want to run it through something like U2U's CAML Query Builderto double check. The error message is thrown by SharePoint before the requested casts. Glancing over it, I think you have an extra <And>at the beginning (<Where><And><And>)

从错误消息看来,CAML 查询是错误的。您可能希望通过U2U 的 CAML 查询生成器之的工具运行它以进行仔细检查。错误消息在请求的强制转换之前由 SharePoint 引发。扫了一眼,我觉得你<And>开头多了个(<Where><And><And>

By the way: Don't use SPWeb.Lists[Name]. This will load every list in the SPWeb (including Metadata), which is rather resource intensive. One of the SPWeb.GetList or SPWeb.Lists.GetList methods is better.

顺便说一句:不要使用 SPWeb.Lists[Name]。这将加载 SPWeb 中的每个列表(包括元数据),这是资源密集型的。SPWeb.GetList 或 SPWeb.Lists.GetList 方法之一更好