spring.net表达式计算器 - 拼写 - 列表迭代

时间:2012-04-15 05:57:58

标签: c# list spring.net

我正在尝试在SpEL中执行以下行但它不起作用:

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

其中FileAttachments是类FileAttachment的对象列表,其属性为DownloadedPath

基本上我正在尝试检查FileAttachment的{​​{1}}属性是否包含“CMA”

但它返回错误:

  

选择只能用于实现类型的实例   IEnumerable的。

1 个答案:

答案 0 :(得分:1)

我创建了一个简单的原型,因为我认为你的表达应该有效,而且确实有效:

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

根据您的错误消息,我认为您的FileAttachment类与您描述的不同。最后,您将列表传递给表达式,而不是包含列表的容器对象。