c# - OrderBy on a Sub Collection using RavenDB -
i'm using ravendb within c# asp.net, , i'm having trouble applying orderby orders value within sub collection. basic example of model is:
public class record { public string id { get; set; } public string name {get; set; } public field[] fields { get; set; } } public class field { public string name { get; set; } public string value { get; set; } }
this saves down ravendb so
{ "name": "a name of somesort" "fields": [ { "name": "colour", "value": "red" }, { "name": "size", "value": "large" } ] }
imagining large amount of these records saved db, can page of data so
using (var session = documentstore.opensession("testdb")) { var result = session.query<record>().skip(0).take(10).tolist(); }
what i'd able sort records based on field value (for example: sort records on value of field name 'colour')
edit:
to explain further, im wanting achieve below, not allowed in ravendb need find alternative way of doing (if possible)
using (var session = documentstore.opensession("testdb")) { var result = session.query<record>() .orderby(v => v.fields.first(f => f.name == "colour").value) .skip(0) .take(10) .tolist(); }
this throws new argumentexception following message
invalid computation: v.fields.first(f => (f.name == "colour")).value. cannot use computation (only simple member expression allowed) in ravendb queries.
so, ive managed working, sharing how did it.
i had create new index below
public class record_byfield : abstractindexcreationtask<record> { public record_byfield() { map = records=> r in records select new { _ = r.fields .select(field => createfield(field.name, field.value, false, true)) }; } }
this meant use lucenequery orderby on field value (in example, on values in colour field)
var result = session.advanced.lucenequery<record>("record/byfield") .orderby("+colour") .skip(0) .take(10) .tolist();
to order descending, can replace + prefix - prefix (-colour), , can pass field name order by.
for information on progmmatically installing indexes on specific database - see programmatically create index
for information on dynamic indexes - see http://ravendb.net/docs/2.0/client-api/advanced/dynamic-fields
Comments
Post a Comment