如何查询SPView对象 [英] How to query the SPView object

查看:156
本文介绍了如何查询SPView对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量 SPListItem 对象的 SPView 对象(有视图中的许多领域)



我只有在这些领域之一感兴趣。让我们把它叫做 specialField



由于观点和specialField,我想知道如果一个值包含在specialField。



下面就是做我想做的事情的一种方式:

 字符串specialField =特殊字段; 
字符串specialValue =值;
SPList列表= SPContext.Current.Site.RootWeb.Lists [我的列表];
SPView视图= list.Views [我的观点]; //这是我想查询

SPQuery查询=新SPQuery()的观点;
query.Query = view.Query;
SPListItemCollection项目= list.GetItems(查询);
的foreach(在项目SPListItem项目)
{
VAR值=项目[specialField]
如果(值!= NULL)及和放大器; (value.ToString()== specialValue)
{
//我的价值发现。这就是我一直在寻找。
//跳出循环或返回
}
}

//我的价值是找不到的。



然而,通过各列表项的迭代似乎不是最优的,尤其是因为可能有几百个项目。该查询将被执行的次数,所以我在寻找一种有效的方式来做到这一点。



修改
我不会永远用相同的视图中工作,所以我的溶液不能被硬编码(它必须是足够的列表中,视图和specialField可以改变通用



会是更好的?它转换为一个IEnumerable对象,说是这样的:

  list.GetItems(查询).Cast< SPListItem>()。其中,(项目=> 
{
回报率((项目[specialField] = NULL)及!及(项[specialField]的ToString()== specialValue));
} ).Count之间的()大于0,

这将是更有效的还是我在错误的标题?方向完全


解决方案

 字符串specialField =特殊字段; 
字符串specialValue =值;
SPList列表= SPContext.Current.Site.RootWeb.Lists [我的列表];
SPView视图= list.Views [我的观点]; //这是认为我想查询

SPQuery查询=新SPQuery();
串TMP = view.Query;
如果(tmp.Contains(<其中>)){
//包装现有的在您需要的子句中子句(它应该是一个,我认为)
TMP = TMP .Insert(tmp.IndexOf(&下;其中>中)+(&下;其中>中的长度),&所述;和>&下;等式>&下; FieldRef名称='+ specialField +'/>&下;值类型=文本>中+ specialValue +< /价值与GT;< / EQ>);
TMP = tmp.Insert(tmp.IndexOf(< /在哪里>中),< /和>);
}其他{
//添加一个where子句,如果一个犯规存在
TMP =<其中><公式>< FieldRef名称='+ specialField +'/> <值类型=文本>中+ specialValue +< /价值与GT;< / EQ>< /在哪里>中+ TMP;
}
query.Query = tmp目录;
SPListItemCollection项目= list.GetItems(查询); {
//我的价值发现;如果(0 item.Count>)
。这就是我一直在寻找。
//跳出循环或返回
}其他{
//我的价值是找不到的。
}


I have a SPView object that contains a lot of SPListItem objects (there are many fields in the view).

I am only interested in one of these fields. Let's call it specialField

Given that view and specialField, I want to know if a value is contained in specialField.

Here is a way of doing what I want to do :

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.

However, iterating through each ListItem hardly seems optimal, especially as there might be hundreds of items. This query will be executed often, so I am looking for an efficient way to do this.

EDIT I will not always be working with the same view, so my solution cannot be hardcoded (it has to be generic enough that the list, view and specialField can be changed.

Would it better to cast it to an IEnumerable object? Say something like this :

list.GetItems(query).Cast<SPListItem>().Where(item => 
{
    return ((item[specialField] != null) && (item[specialField].ToString() == specialValue));
}).Count() > 0;

Would this be more efficient or am I heading in the wrong direction entirely?

解决方案

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.
}

这篇关于如何查询SPView对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆