2

i have LINQ expression node type ArrayIndex is not suported in LINQ to Entities error when i am trying to do the following

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;
    }
} 

As you can see that i am trying to split the Attachmentfilepath which contains '$' and assign the second value([1]) to the Attachment_FilePath

Can anyone please suggest how i can split and assign the value to the AttachmentList string in the same query Thanks

nemesv
  • 138,284
  • 16
  • 416
  • 359
bhargav
  • 619
  • 2
  • 14
  • 30

2 Answers2

2

To be honest, the simplest approach would be to do the splitting on the client-side, unless you really need it to be a fully-fledged entity. For example:

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();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • hi Jon.Thanks very much for the fast reply you have given.Well i was searching for a single step solution.I think this will solve my problem.Thanks again – bhargav Feb 13 '12 at 14:18
2

You can project to an anonymous class first to grab the data you need, then switch to using Linq to objects where this kind of operation is supported using 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();
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335