+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Dynamic type where filter on a "Choices" type?

Dynamic type where filter on a "Choices" type?

16 posts, 1 answered
  1. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    18 Jul 2012
    Link to this post
    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'

  2. Dimitar Dimitrov
    Dimitar Dimitrov avatar
    35 posts
    Registered:
    02 Dec 2016
    19 Jul 2012
    Link to this post
    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
  3. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    19 Jul 2012
    Link to this post
    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?
  4. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    19 Jul 2012
    Link to this post
    ..got another for you

    So I have an Guid Array column...how do I return only those values on a filter?
  5. Stoimen Stoimenov
    Stoimen Stoimenov avatar
    60 posts
    Registered:
    11 Jul 2016
    24 Jul 2012
    Link to this post
    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
  6. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    24 Jul 2012
    Link to this post
    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)
  7. Stoimen Stoimenov
    Stoimen Stoimenov avatar
    60 posts
    Registered:
    11 Jul 2016
    26 Jul 2012
    Link to this post
    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
  8. Stoimen Stoimenov
    Stoimen Stoimenov avatar
    60 posts
    Registered:
    11 Jul 2016
    30 Jul 2012
    Link to this post
    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
    Answered
  9. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    30 Jul 2012
    Link to this post
    Thanks Stoimen!
  10. Stoimen Stoimenov
    Stoimen Stoimenov avatar
    60 posts
    Registered:
    11 Jul 2016
    30 Jul 2012
    Link to this post
    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
  11. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    13 Nov 2014 in reply to Stoimen Stoimenov
    Link to this post

    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

  12. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    24 Nov 2016
    18 Nov 2014
    Link to this post
    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
     
  13. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    19 Nov 2014 in reply to Nikola Zagorchev
    Link to this post

    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

  14. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    24 Nov 2016
    20 Nov 2014
    Link to this post
    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
     
  15. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    20 Nov 2014 in reply to Nikola Zagorchev
    Link to this post

    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

  16. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    24 Nov 2016
    20 Nov 2014
    Link to this post
    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
     
16 posts, 1 answered