2012-04-15 16 views
0

我試圖執行在規劃環境地政司下面的行,但它不工作:spring.net表達式求值,spel-列表迭代

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

哪裏FileAttachments是其財產被DownloadedPathFileAttachment類的對象列表。

基本上我試圖檢查是否有FileAttachmentDownloadedPath屬性包含「CMA」

但它返回錯誤:

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

回答

1

我創建了一個簡單的原型,因爲我有你的表情應該工作oppinion而事實上它的工作:

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類與你描述的不同。最終,您將該列表傳遞給表達式,而不是容納列表的容器對象。