Dynamic type where filter on a "Choices" type?
So I can't do something like this:
var items =
this
.RetrieveCollectionOfProComps();
mf1ListView.DataSource = items.Where(
"MF = \"1\""
);
Hi Steve,
The problem is that the Choice field always returns an array with the selected values for the given item. That is why your compare is failing.
var selectedValues = item.GetValue<
string
[]>(
"choiceFieldName"
);
Hey Dimitar,
Sorry maybe a bit of mis-communication on my part :)
What I need to do is put the query into the where clause to filter out only items with choice X. The below code in a sample, but I'd be putting it into the "RetrieveCollectionOfProComps" method as its an IQueryable OA should do the filtering sql side.
Doesn't make sense to me to pull down 2000 records, then loop through each to get the "choiceFieldName", THEN do a where clause on that.
Know what I mean?
What's the filter string for a choice?
..got another for you
So I have an Guid Array column...how do I return only those values on a filter?
Hello Steve,
I did some testing and it appears that there is no way to query items filtered by choices field directly because OpenAccess is not supporting it. However for every other field this can be achieved easily. I would advice you to switch to a custom taxonomy instead of choices field and after that you can filter items using this expression:
.Where(CustomTaxonomyField.Contains(\"A9F2C13A-9472-4BF6-AB3D-19B2EF05B043\"));
var myFilteredCollection = dynamicModuleManager.GetDataItems(testContentType).Where(item => item.GetValue<
Guid
[]>("ArrayOfGuidsField").Contains(myGuid));
Eugh....really? :/
You guys work pretty close with the OA guys now...can you file this as a bug with them? It's CRAZY inefficient, and we can't use taxons for this (they shouldn't be, and we don't want to bloat the taxon pool)
Hi,
We will definitely provide a solution for filtering items by choices fields in future releases but for now I can't suggest any other workaround except using taxonomies. I'll talk to the OA guys and I'll try to provide you with a pits issue shortly.
All the best,Hello,
I contacted the OA team and they will try to fix it for the next release. However here is the pits issue so that you can vote for it to increase its priority and to track its progress. It turned out that there have been a similar issue with array of guids field and it has been fixed so hopefully and one will be resolved as soon as possible.
Greetings,
Stoimen Stoimenov
the Telerik team
Thanks Stoimen!
Hello,
Some follow up. OA guys will fix this for the next release but in the meantime they showed me a very simple workaround that might seem a little bit strange but it should work in most (or maybe even all) situations.
To get this working:
dynamicModuleManager.GetDataItems(content2Type).Where(i => i.FieldValue<
string
[]>(
"ChoicesField"
).Contains(
"Second Choice"
));
string
choiceOption =
"Second Choice"
;
var myFilteredCollection = dynamicModuleManager.GetDataItems(content2Type).Where(i => i.FieldValue<
string
[]>(
"ChoicesField"
).Contains(choiceOption));
using
Telerik.OpenAccess;
So this appears to be solved in the current version(s).
How would I check on multiple values in this case? So if I have a ChoiceField then I get a string[] with one or more values. Can I do this within one statement?
Best,
Daniel
Hi
You can use the following filter expression to filter the items using the API, as well as, the data service(data.svc):
var providerName = String.Empty;
var searchedValues =
new
string
[]
"Option2"
;
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
Type testType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.Tests.Test"
);
// filter by choicefield
var testItems = dynamicModuleManager.GetDataItems(testType).Where(@
"Visible = true AND Options.Contains("
"2"
")"
)
.Where(t=> t.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);
var testItems2 = dynamicModuleManager.GetDataItems(testType)
.Where(@
"Visible = true AND Options.Contains("
"2"
") AND Options.Contains("
"1"
")"
)
.Where(t => t.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);
Thanks Nikola, that worked great!
Any idea why this line of code also returns the items that are inside the Recycle Bin?
referenceCollection = _dynamicModuleManager.GetDataItems(_referenceType).Where(@
"ReferenceType.Contains("
"1"
")"
).Where(x => x.Status == ContentLifecycleStatus.Live);
If I filter on ContentLifecycleStatus.Deleted, I get the item from the Recycle Bin, but it also appears when I execute the above code.
Is this a known problem?
Best,
Daniel
Hi Daniel,
You should filter by Visible, as well. It will be true if the item is publicly visible. You can find more information on this API change here.
Regards,
Nikola Zagorchev
Telerik
Hi Nikola,
Thanks for the follow up. That worked great. Is it by design that you can't filter on the Status? I assume that an item would have the Deleted status once it is inside the Recycle Bin?
Best,
Daniel
Hello Daniel,
This is by design and all widgets default filtering is change to match both the visible and status properties. You can check the default filters and building filter expression in our documentation on the following link.
Regards,
Nikola Zagorchev
Telerik