Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何查询SPView对象_C#_Sharepoint_Sharepoint 2007_Wss 3.0 - Fatal编程技术网

C# 如何查询SPView对象

C# 如何查询SPView对象,c#,sharepoint,sharepoint-2007,wss-3.0,C#,Sharepoint,Sharepoint 2007,Wss 3.0,我有一个SPView对象,它包含许多splistem对象(视图中有许多字段) 我只对其中一个领域感兴趣。我们称之为specialField 鉴于该视图和specialField,我想知道specialField中是否包含值 以下是我想做的事情的一种方法: String specialField = "Special Field"; String specialValue = "value"; SPList list = SPContext.Current.Site.RootWeb.Lists["

我有一个
SPView
对象,它包含许多
splistem
对象(视图中有许多字段)

我只对其中一个领域感兴趣。我们称之为
specialField

鉴于该视图和specialField,我想知道specialField中是否包含值

以下是我想做的事情的一种方法:

String specialField = "Special Field";
String specialValue = "value";
SPList list = SPContext.Current.Site.RootWeb.Lists["My List"];
SPView view = list.Views["My View"]; //This is the view I want to query

SPQuery query = new SPQuery();
query.Query = view.Query;
SPListItemCollection items = list.GetItems(query);
foreach(SPListItem item in items)
{
    var value = item[specialField];
    if(value != null) && (value.ToString() == specialValue)
    {
        //My value is found. This is what I was looking for.
        //break out of the loop or return
    }
}

//My value is not found.
然而,遍历每个ListItem似乎不是最优的,尤其是可能有数百个项目。此查询将经常执行,因此我正在寻找一种有效的方法来执行此操作

编辑 我不会总是使用相同的视图,因此我的解决方案不能硬编码(它必须足够通用,以便可以更改列表、视图和特殊字段)

将其投射到IEnumerable对象是否更好?请这样说:

list.GetItems(query).Cast<SPListItem>().Where(item => 
{
    return ((item[specialField] != null) && (item[specialField].ToString() == specialValue));
}).Count() > 0;
list.GetItems(query.Cast()。其中(item=>
{
返回((项[specialField]!=null)和&(项[specialField].ToString()==specialValue));
}).Count()>0;

这是更有效还是我完全走错了方向?

您可以在Caml中执行查询。

是理解Caml中查询的好链接,也是自动生成查询的软件链接。

String specialField=“Special Field”;
String specialField = "Special Field";
String specialValue = "value";
SPList list = SPContext.Current.Site.RootWeb.Lists["My List"];
SPView view = list.Views["My View"]; //This is the view I want to query

SPQuery query = new SPQuery();
string tmp = view.Query;
if(tmp.Contains("<Where>")) {
    //wrap the existing where clause in your needed clause (it should be an And i think)
    tmp = tmp.Insert(tmp.IndexOf("<Where>") + ("<Where>".Length), "<And><Eq><FieldRef Name='"+specialField+"'/><Value Type='Text'>"+specialValue+"</Value></Eq>");
    tmp = tmp.Insert(tmp.IndexOf("</Where>"), "</And>");
} else {
    //add a where clause if one doesnt exist
    tmp = "<Where><Eq><FieldRef Name='"+specialField+"'/><Value Type='Text'>"+specialValue+"</Value></Eq></Where>" + tmp;
}
query.Query = tmp;
SPListItemCollection items = list.GetItems(query);
if(item.Count > 0) {
    //My value is found. This is what I was looking for.
    //break out of the loop or return
} else {
    //My value is not found.
}
字符串specialValue=“value”; SPList list=SPContext.Current.Site.RootWeb.Lists[“我的列表”]; SPView view=list.Views[“我的视图”];//这是我要查询的视图 SPQuery query=新建SPQuery(); 字符串tmp=view.Query; if(tmp.Contains(“”){ //将现有where子句包装到所需子句中(我认为它应该是And) tmp=tmp.Insert(tmp.IndexOf(“”+(“”.Length),“”+specialValue+); tmp=tmp.Insert(tmp.IndexOf(“”“”); }否则{ //如果不存在where子句,则添加where子句 tmp=“”+特殊值+”+tmp; } query.query=tmp; SPListItemCollection items=list.GetItems(查询); 如果(item.Count>0){ //我的价值被发现了。这就是我一直在寻找的。 //跳出循环或返回 }否则{ //找不到我的值。 }
我已经在做一个CAML查询来从视图中获取SPListItems…有没有办法直接在SPView对象上进行CAML查询?如果我必须先使用CAML从列表中获取SPListItems,然后再使用CAML来查询该集合,我不确定它是否会如此高效。你有没有一个例子,记住我正在使用的是作为基本集查看,而不是列表。为什么不在查询中附加一个子句呢。举个例子,因为我不知道查询是什么样子的(即视图查询)。如果有一个OrderBy子句,例如,我不能在后面添加where子句(它会给出一个………但这不起作用。Where子句看起来像…字段比较字段+顺序,CAML就像XML一样工作,即使它看起来像多个根节点,但只有发送到SPQuery的整个内容被SPView对象封装在一个标记中,只要在Google上查找SharePoint CAML就可以了。您的标题是错误的。)(Action!;)问题的根源是检索一个包含大部分不相关记录的大型记录集,然后在其中循环。甚至可以转换为IEnumerable和do.Where()仍在暗中进行。使用SharePoint提供的功能-找出如何将WHERE添加到CAML中,使其有效,即使您已收到order by等。尚未测试,但从我看来很好,so+1。另一种方法是将CAML放入XmlDocument中,并通过DOM进行解析/修改。是的,这看起来就像我所说的那样我必须查询一次。让我让这个工作,我会标记为接受。谢谢你的帮助!这工作得很好。不过需要注意的是:在第一个if中,第一行应该以tmp=tmp.Insert(tmp.IndexOf(“”+)(“”.Length)…(我删除了+1)开始,这样它才能正常工作。再次感谢!