本文介绍了使用 SPQuery 检索不同/唯一值的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的列表:

I have a list that looks like:

Movie          Year
-----          ----
Fight Club     1999
The Matrix     1999
Pulp Fiction   1994

使用 CAML 和 SPQuery 对象,我需要从 Year 列中获取一个不同的项目列表,该列表将填充下拉控件.

Using CAML and the SPQuery object I need to get a distinct list of items from the Year column which will populate a drop down control.

在 CAML 查询中四处搜索似乎没有办法做到这一点.我想知道人们是如何实现这一目标的?

Searching around there doesn't appear to be a way of doing this within the CAML query. I'm wondering how people have gone about achieving this?

推荐答案

另一种方法是使用 DataView.ToTable-Method - 它的第一个参数是使列表不同的参数.

Another way to do this is to use DataView.ToTable-Method - its first parameter is the one that makes the list distinct.

            SPList movies = SPContext.Current.Web.Lists["Movies"];
            SPQuery query = new SPQuery();
            query.Query = "<OrderBy><FieldRef Name='Year' /></OrderBy>";

            DataTable tempTbl = movies.GetItems(query).GetDataTable();
            DataView v = new DataView(tempTbl);
            String[] columns = {"Year"};
            DataTable tbl = v.ToTable(true, columns);

然后您可以继续使用 DataTable tbl.

You can then proceed using the DataTable tbl.

这篇关于使用 SPQuery 检索不同/唯一值的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 18:35
查看更多