2012-02-13 31 views
2

我有LINQ表達式節點類型ArrayIndex沒有在LINQ支載到實體錯誤,當我嘗試做以下LINQ查詢裏面關於ArrayIndex

public List<AttachmentList> ShowAttachments(int type, int ReferenceId) 
{ 

    try 
    { 
     var attachmentNames = 
      (from attachment in tent.Attachments 
       where (attachment.Attachment_Type == type 
        && attachment.Attachment_Reference_Pk == ReferenceId) 
       select new AttachmentList 
         { 
          Attachment_Pk = attachment.Attachment_Pk, 
          Attachment_File_Path = attachment 
           .Attachment_File_Path.Split(new[] {'$'})[1] 
         }).ToList(); 

     return attachmentNames; 
    } 
    catch (Exception ex) 
    { 
     ExceptionHandler.ExceptionLog(ex); 
     return null; 
    } 
} 

正如你可以看到,我試圖分裂Attachmentfilepath其中包含'$'並指定第二個值([1])到Attachment_FilePath

任何人都可以請建議我怎麼可以分割並且在相同的查詢賦值給AttachmentList串 感謝

+0

感謝nemesev編輯 – bhargav 2012-02-13 14:17:35

回答

2

說實話,最簡單的方法是在客戶端進行分割,除非你真的需要它是一個完整的實體。例如:

var query = from attachment in tent.Attachments 
      where attachment.Attachment_Type == type && 
        attachment.Attachment_Reference_Pk == ReferenceId 
      select new { Attachment_Pk, Attachment_File_Path }; 

// Force the rest to execute client-side. 
var attachmentNames = query.AsEnumerable() 
          .Select(x => new AttachmentList { 
           Attachment_Pk = x.Attachment_Pk, 
           Attachment_File_Path = x.Attachment_File_Path 
                 .Split('$')[1] 
          }) 
          .ToList(); 
+0

您好Jon.非常感謝您給出的快速回復。我正在尋找單步解決方案。我認爲這將解決我的問題。再次感謝 – bhargav 2012-02-13 14:18:36

2

可以投射到一個匿名類首先要抓住你所需要的數據,然後切換到使用LINQ to對象,其中這種操作支持使用AsEnumerable()

var attachmentNames = (from attachment in tent.Attachments 
         where (attachment.Attachment_Type == type && attachment.Attachment_Reference_Pk == ReferenceId) 
         select new { attachment.Attachment_Pk, attachment.Attachment_File_Path }) 
         .AsEnumerable() 
         .Select(attachment => 
         new AttachmentList 
         { 
          Attachment_Pk = attachment.Attachment_Pk, 
          Attachment_File_Path = attachment.Attachment_File_Path.Split(new[] { '$' })[1] 
         }).ToList(); 
+0

這也是一個很好的解決方案。感謝Brokenglass – bhargav 2012-02-13 14:25:47