我正在尝试使用 linq 返回包含另一个对象列表作为数据成员的对象列表。我已经尝试过显示的示例,但每次尝试都会出现不同的错误。其中之一如下:LINQ to Entities 无法识别方法 'System.Collections.Generic.List 1[SunGard.Tools.Notifications.LinkVariable] ToList[LinkVariable](System.Collections.Generic.IEnumerable
1[SunGard.Tools.Notifications.LinkVariable])' 方法,并且该方法无法转换为存储表达式。
我有一个对象(AlertMessageReturn),其中包含一些字符串数据成员以及另一个对象(列表)的列表。我有一个定义 LinkVarible 的类和一个包含值的表。我的查询如下所示:
AlertMessagesQuery = from alertMessage in this.context.AlertMessages
where alertMessage.UserId=UserId
select new AlertMessageReturn()
{ PAM_ShortMessage = alertMessage.PAM_ShortMessage,
PAM_LongMessage = alertMessage.PAM_LongMessage,
PAM_LongMessageRemote = alertMessage.PAM_LongMessageRemote,
LinkVariables = (from linkVariable in this.context.AlertMessageLinks
from user in this.context.AlertMessageUsers
where user.PAMU_PAM_ID == linkVariable.PAML_PAM_ID && user.PAMU_UserId == UserId
select new LinkVariable()
{
Name = linkVariable.PAML_SessionVariableName,
Value = linkVariable.PAML_SessionVariableValue
})
};
该错误与为链接变量返回的类型有关。请帮忙。
我将代码更改如下:
LinkDataQuery = from linkData in this.context.AlertMessageLinks
from user1 in this.context.AlertMessageUsers
where user1.PAMU_PAM_ID == linkData.PAML_PAM_ID && user1.PAMU_UserId == UserId
select new LinkData
{
Name = linkData.PAML_SessionVariableName,
Value = linkData.PAML_SessionVariableValue
};
var links = LinkDataQuery.ToList();
AlertMessagesQuery = from alertMessage in this.context.AlertMessages
where alertMessage.UserId=UserId
select new AlertMessageReturn()
{ PAM_ShortMessage = alertMessage.PAM_ShortMessage,
PAM_LongMessage = alertMessage.PAM_LongMessage,
PAM_LongMessageRemote = alertMessage.PAM_LongMessageRemote,
LinkVariables = links
};
var AlertMessages = AlertMessagesQuery.ToList(); // this is where the error point to
if (AlertMessages.Any())
{
return AlertMessages;
}
我现在得到的错误是:System.NotSupportedException:无法创建类型为“SunGard.Tools.Notifications.LinkData”的常量值。此上下文仅支持原始类型(“例如 Int32、String 和 Guid”)。