0

I am trying to execute the following line in SpEL but it does not work:

FileAttachments.?{DownloadedPath.Contains('CMA')}.count()>0

Where FileAttachments is a list of objects of class FileAttachment whose property is DownloadedPath.

Basically I am trying to check if any FileAttachment's DownloadedPath property contains "CMA".

But it returns with an error:

Selection can only be used on an instance of the type that implements IEnumerable.

Andreas
  • 5,251
  • 30
  • 43

1 Answers1

1

I created a simple prototype as I had the oppinion that your expression should work and indeed it does work:

using System.Collections.Generic;
using System.Diagnostics;
using Spring.Expressions;

namespace StackOverflow10159903
{
  internal class Program
  {
    private static void Main(string[] args)
    {
      var attachmentContainer = new FileAttachmentsContainer();
      attachmentContainer.AddAttachment(new FileAttachment {DownloadedPath = "CMA"});

      var attachments = new List<FileAttachment>();
      attachments.Add(new FileAttachment {DownloadedPath = "CMA"});

      var valueFromList =
        ExpressionEvaluator.GetValue(attachments, "?{DownloadedPath.Contains('CMA')}.count()>0") 
        as bool?;

      var valueFromContainer =
        ExpressionEvaluator.GetValue(attachmentContainer, "FileAttachments?{DownloadedPath.Contains('CMA')}.count()>0")
        as bool?;

      Debug.Assert(valueFromList == true);
      Debug.Assert(valueFromContainer == true);
    }
  }

  public class FileAttachmentsContainer
  {
    private readonly List<FileAttachment> _fileAttachments;

    public FileAttachmentsContainer()
    {
      _fileAttachments = new List<FileAttachment>();
    }

    public IEnumerable<FileAttachment> FileAttachments
    {
      get { return _fileAttachments; }
    }

    public void AddAttachment(FileAttachment fileAttachment)
    {
      _fileAttachments.Add(fileAttachment);
    }
  }

  public class FileAttachment
  {
    public string DownloadedPath { get; set; }
  }
}

According to your error message I suppose your FileAttachment class differs from what you describe. Eventually you are passing the list to the expression and not a container object holding the list.

Andreas
  • 5,251
  • 30
  • 43