关于Linq Query中的ArrayIndex

时间:2012-02-13 14:04:53

标签: c# linq-to-entities

当我尝试执行以下操作时,LINQ表达式节点类型ArrayIndex未在LINQ to Entities错误中被支持

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}}并将第二个值([1])分配给'$'

任何人都可以建议我如何拆分并将值分配给同一查询中的AttachmentList字符串 感谢

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:2)

您可以首先投射到匿名类以获取所需的数据,然后切换到使用Linq到使用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();