当我尝试执行以下时,LINQ到实体的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字符串吗感谢
老实说,最简单的方法是在客户端进行拆分,除非真的需要它成为一个成熟的实体。例如:
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();
您可以先投影到一个匿名类来获取所需的数据,然后切换到使用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();