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

Forums / Bugs & Issues / Filtering Images

Filtering Images

5 posts, 0 answered
  1. martin
    martin avatar
    36 posts
    Registered:
    30 Nov 2008
    30 Jun 2011
    Link to this post

    Hi,

    I'm creating a control that retrieves images from a list of image libraries. I need to filter the images returned by a keyword, which I compare against the Name and Description of the image.


    LibraryManager lbm = new LibraryManager("Libraries");
    ContentManager  manager = new ContentManager("Libraries");
     
    List<IMetaSearchInfo> imageFilters = new List<IMetaSearchInfo>();
    MetaSearchInfo filter1 = new MetaSearchInfo(MetaValueTypes.ShortText, "Name", "str", SearchCondition.Like, JoinType.Or);
    imageFilters.Add(filter1);
    MetaSearchInfo filter2 = new MetaSearchInfo(MetaValueTypes.ShortText, "ImgDesc", "str", SearchCondition.Like, JoinType.Or);
    imageFilters.Add(filter2);
     
    Guid[] libraries = new Guid[]{new Guid("D4ABDB26-86CE-434F-A4E7-1635FF768E17")};
    IList images = manager.GetContent(0, 0, imageFilters.ToArray(), libraries);


    Issue:
    when I remove filter2, I get 1 image returned with is correct. when I remove filter1 I get 0 images returned which is correct, but when I have both filters present I get the same image returned 12 times.

    Am I missing something in my code.

    Basically I'm looking to select all images that are in Library D4ABDB26-86CE-434F-A4E7-1635FF768E17 where Name of the image is like "str" or  the imgDesc is like "str"

    Any help would be appricated
    Thanks
    Kieran

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    01 Jul 2011
    Link to this post
    Hello Martin,

    Try using the following sample code which returns correct result at my end

    var ParentID = new Guid("d29b5a68-44f0-4e70-91d8-caef7a22873c");
    var manager = new ContentManager("Libraries");
    var result = manager.GetContent().Cast<IContent>().ToList().Where(cnt => cnt.ParentID == ParentID && cnt.GetMetaData("Name").ToString().Contains("msp"));


    All the best,
    Ivan 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. martin
    martin avatar
    36 posts
    Registered:
    30 Nov 2008
    01 Jul 2011
    Link to this post
    Thanks Ivan,

    That worked, but only for a single search word, my issue now is that I need to dynamically create the where clause portion of the query. I could have a few words to search against the name.

    My original approach allowed me to create multiple filters and then add them to the getContent method.

    Is there anyway to do this with your approach.

    Eg
    manager.GetContent().Cast<IContent>().ToList().Where(cnt =>
    cnt.ParentID == ParentID &&
    cnt.GetMetaData("Name").ToString().Contains("msp") && cnt.GetMetaData("Name").ToString().Contains("term2") && cnt.GetMetaData("Name").ToString().Contains("term3"));
    .

    Thanks
    Martin
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    01 Jul 2011
    Link to this post
    Hello Martin,

    You can create a helper method that filters the data based on your paremeters

    public IList<IContent> GetMatchedResult(string[] parameters, ContentManager manager)
      {
          var matchedData = new IList<IContent>();
          var allItems = manager.GetContent(); // you can apply initial filter
          var number = parameters.Count(); //5
          foreach (IContent itm in allItems)
          {
              int counter = 0;
              for (int i = 0; i < number; i++)
              {
                  var medataDataValue = itm.GetMetaData("Name").ToString();
                  if (medataDataValue.Contains(parameters[i]))
                  {
                      counter++;
     
                  }
                  else
                  {
                      break;
                  }
                  if (counter == number)
                  {
                      matchedData.Add(itm);
                  }
              }
          }
          return matchedData;
      }

    or create a class that will make your builder expression in LINQ.

    Greetings,
    Ivan 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
  5. martin
    martin avatar
    36 posts
    Registered:
    30 Nov 2008
    04 Jul 2011
    Link to this post
    Thanks,

    I took your approach seems to work

    Martin
Register for webinar
5 posts, 0 answered