0

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

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

where是属性为FileAttachments的类对象的列表。FileAttachmentDownloadedPath

基本上我正在尝试检查是否有任何属性包含FileAttachment" CMA"DownloadedPath

但它返回错误:

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

4

1 回答 1

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课程与您描述的不同。最终,您将列表传递给表达式,而不是保存列表的容器对象。

于 2012-04-15T10:45:10.830 回答