Dynamic type where filter on a "Choices" type?

Posted by Community Admin on 04-Aug-2018 13:06

Dynamic type where filter on a "Choices" type?

All Replies

Posted by Community Admin on 18-Jul-2012 00:00

So I can't do something like this:

var items = this.RetrieveCollectionOfProComps();
mf1ListView.DataSource = items.Where("MF = \"1\"");

What's the syntax?  I've tried a bunch of things, just unsure what it's supposed to be...

Operator '=' incompatible with operand types 'String[]' and 'String'

Posted by Community Admin on 19-Jul-2012 00:00

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");


Kind regards,
Dimitar Dimitrov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 19-Jul-2012 00:00

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?

Posted by Community Admin on 19-Jul-2012 00:00

..got another for you

So I have an Guid Array column...how do I return only those values on a filter?

Posted by Community Admin on 24-Jul-2012 00:00

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\"));

As for the array of guids field you can use the following:

var myFilteredCollection = dynamicModuleManager.GetDataItems(testContentType).Where(item => item.GetValue<Guid[]>("ArrayOfGuidsField").Contains(myGuid));


All the best,
Stoimen Stoimenov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 24-Jul-2012 00:00

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)

Posted by Community Admin on 26-Jul-2012 00:00

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,
Stoimen Stoimenov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 30-Jul-2012 00:00

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

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 30-Jul-2012 00:00

Thanks Stoimen!

Posted by Community Admin on 30-Jul-2012 00:00

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"));

simply assign the "Second Choice" option to a string variable and pass it to to the contains method like that:

string choiceOption = "Second Choice";
            var myFilteredCollection = dynamicModuleManager.GetDataItems(content2Type).Where(i => i.FieldValue<string[]>("ChoicesField").Contains(choiceOption));

And the items are properly filtered.

For .FieldValue extension method you need to add the following using:

using Telerik.OpenAccess;


All the best,
Stoimen Stoimenov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 13-Nov-2014 00:00

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

Posted by Community Admin on 18-Nov-2014 00:00

Hi Daniel,

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);

In order to be able to use this extension method, please include using to the Telerik.Sitefinity.Data.Linq.Dynamic;

Regards,
Nikola Zagorchev
Telerik
 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

Posted by Community Admin on 19-Nov-2014 00:00

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

Posted by Community Admin on 20-Nov-2014 00:00

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

 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

Posted by Community Admin on 20-Nov-2014 00:00

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

Posted by Community Admin on 20-Nov-2014 00:00

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

 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

This thread is closed