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

本文关键字:列表 迭代 计算器 表达式 net spring | 更新日期: 2023-09-27 18:31:13

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

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

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

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

但它返回一个错误:

选择只能用于实现 IE无数。

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

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

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