2

我想查询一个valueList通过网络服务调用的 Sharepoint 2007 列表,并找到每个列表项的最新版本,所以给出了这个列表:

ID | Value
----------
1  | 101
2  | 305
3  | 102
4  | 101
5  | 305
6  | 101

我希望我的查询返回:

ID | Value
----------
3  | 102
5  | 305
6  | 101

在 Access SQL 中,我会这样做:

select v1.* from valueList v1
inner join 
(select value, max(id) as maxID from valueList group by value) v2
on v1.id=v2.maxID

到目前为止,似乎我可以在 C#/CAML/Sharepoint Web 服务中执行此操作的唯一方法是运行 CAML 查询来对列表项进行分组value并按顺序排序ID,将查询转储到 DataTable,然后遍历每一行DataTable,为每个value. 代码如下:

//dataHandler constructor initializes Web Services Lists() object
dataHandler dh = new dataHandler();

string[] fields = { "ID", "value"};
string query =
           "<GroupBy collapse='true'>" +
              "<FieldRef Name='value' />" +
           "</GroupBy>" +
           "<OrderBy>" +
              "<FieldRef Name='ID' />" +
           "</OrderBy>";

DataTable listTable = dh.listToDataTable("valueList", fields, query);

//copy listTable structure to blank resultTable
DataTable resultTable = listTable.Clone();

//initialize value    
int value = (int)Convert.ToDouble(listTable.Rows[0][1]);

for (int ctr = 0; ctr < listTable.Rows.Count; ctr++)
{

    //check to see if we've gone onto a new 'value', if so get previous row
    if (value != (int)Convert.ToDouble(listTable.Rows[ctr][1]) )
    {
        resultTable.ImportRow(listTable.Rows[ctr - 1]);
        value = (int)Convert.ToDouble(listTable.Rows[ctr][1]);
    }

    //get the last row
    if (ctr == listTable.Rows.Count - 1)
    {
        resultTable.ImportRow(listTable.Rows[ctr]);
    }

}

(int)Convert.ToDouble是因为该字段作为字符串存储在 DataTable 中,例如 1234 存储为“1234.0000”。

这似乎不必要地复杂。有没有更直接的方法来完成我所追求的?如果有更简单的方法,我不需要使用 DataTables 或 CAML。

4

2 回答 2

1

看起来您正在列表上进行自己的版本控制?如果您只是使用 SharePoint 版本控制,那么很容易获得最新版本。:)

无论如何,CAML 非常有限,2007 年没有加入(2010 年你有加入)。除非您对列表进行某种预处理。我认为这是不可能的。

于 2012-05-28T01:56:15.207 回答
0

这个链接对我有用:)

DataTable dtList = list.Items.GetDataTable();
var grouped = from row in dtList.AsEnumerable()
          group row by row["Value"] into valueGroup
          select new { Value = valueGroup.Key, ID = valueGroup.Max(id => id["ID"]) };
于 2012-08-18T09:30:57.090 回答