Dynamic Select Distinct

117 views
Skip to first unread message

Elbino

unread,
Apr 2, 2012, 9:06:00 PM4/2/12
to ravendb
I am building a WPF app that's using RavenDB.

The app basically launches and shows the data in a DataGrid.

User's want to be able to filter by grid columns, i.e when they click
a grid column header, I want to display in a drop down list all the
distinct values for that column.

The data is stored in strong typed objects.
currently I create a map-reduce index to select dictinc for each
column

public class QzBookIndex : AbstractIndexCreationTask<VmDto>
{
public QzBookIndex()
{
Map = qzList => from qzObj in qzList
select new { qzObj.Book };

Reduce = results => from result in results
group result by result.Book into g
select new { Book = g.Key };
}

}

then in the filter method I query using the index:
var items = (from item in docSession.Query<VMDto, QzBookIndex>()
select item.CobDate);

All works Fine. But I need to make the index and the select statement
dynamic.

i.e. when the user clicks the header for a particular column in the
grid for the first time, I want to create an index for that property
and then dynamically select all the distinct values for that property
and display them in the dropdown list...

I might be aproaching this the wrong way. but the gist of what I want
is in the paragraph above

Oren Eini (Ayende Rahien)

unread,
Apr 2, 2012, 9:28:41 PM4/2/12
to rav...@googlegroups.com
Don't do that.
Create an index with all of the fields that you are showing.

Then you can use the store.DatabaseCommands.GetTerms() method to get the distinct terms from the index for a particular field in a very cheap fashion.

Elbino

unread,
Apr 2, 2012, 10:10:37 PM4/2/12
to ravendb
Thanks. Exactly what I am looking for.

Can I get an arbitrary page.

Suppose the user selects page 1, then scrolls to page 5...etc

On Apr 2, 9:28 pm, "Oren Eini (Ayende Rahien)" <aye...@ayende.com>
wrote:

Elbino

unread,
Apr 2, 2012, 10:18:17 PM4/2/12
to ravendb
Never mind Got it. Thanks a million

Elbino

unread,
Apr 2, 2012, 10:31:42 PM4/2/12
to ravendb
Please help

IEnumerable<string> GetTerms(string index, string field, string
fromValue, int pageSize);

What would the value of "string fromValue" be in a case where the user
launches the app and goest straight to page 5 or something

On Apr 2, 9:28 pm, "Oren Eini (Ayende Rahien)" <aye...@ayende.com>
wrote:

Matt Warren

unread,
Apr 3, 2012, 5:49:14 AM4/3/12
to rav...@googlegroups.com
In that case, you have to start at the beginning and get the first 5 pages in one query, I don't think there a better way.

Something like this:
    GetTerms("index name", "field name", "start of first page", pageSize * 5); 

Once you've done this you can then carry on paging as normal.

Oren Eini (Ayende Rahien)

unread,
Apr 3, 2012, 5:54:22 AM4/3/12
to rav...@googlegroups.com
Those aren't paged queries, those are unique terms!

Chinedu

unread,
Apr 3, 2012, 7:58:15 AM4/3/12
to rav...@googlegroups.com
Thanks

Matt Warren

unread,
Apr 3, 2012, 8:17:52 AM4/3/12
to rav...@googlegroups.com
Yeah you're right I was getting confused! But I don't see how/why you need to page through the Terms. They are the terms across the entire index, not the ones that are in the current UI page. So you just need all the possible terms, regardless of the page of results that the user is seeing, is that right?

Oren Eini (Ayende Rahien)

unread,
Apr 3, 2012, 11:38:17 AM4/3/12
to rav...@googlegroups.com
Yes, but note that you may have many terms, so you might need to page through that.
Reply all
Reply to author
Forward
0 new messages