gpt4 book ai didi

c# - Entity Framework 中的 Linq 查询错误

转载 作者:太空宇宙 更新时间:2023-11-03 10:56:02 25 4
gpt4 key购买 nike

我正在尝试通过 EF linq 查询从域对象中收集值。但是我收到一个错误,请帮助我更正此问题。

public String[] ReturnPatientIDs(int CounsellingRoomID)
{

var messageObject = this.context.CounsellingMessages.Where(c => c.CounsellingRoomID == CounsellingRoomID).Distinct();

String[] PatientIDs = new String[messageObject.Count()];

for (int k = 0; k < PatientIDs.Length; k++)
{
PatientIDs[k] = messageObject.ElementAt(k).Chatname;
}

return PatientIDs;
}

LINQ to Entities does not recognize the method 'Me.Domain.General.CounsellingMessage ElementAt[CounsellingMessage](System.Linq.IQueryable`1[Me.Domain.General.CounsellingMessage], Int32)' method, and this method cannot be translated into a store expression.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: LINQ to Entities does not recognize the method 'MedicalApp.Domain.General.CounsellingMessage ElementAt[CounsellingMessage](System.Linq.IQueryable`1[MedicalApp.Domain.General.CounsellingMessage], Int32)' method, and this method cannot be translated into a store expression.

Source Error:

Line 43:             for (int k = 0; k < PatientIDs.Length; k++ )
Line 44: {
Line 45: PatientIDs[k] = messageObject.ElementAt(k).Chatname;
Line 46: }
Line 47:

最佳答案

ElementAt 方法不受支持。 messageObject 查询在调用 Count()ElementAt() 之前不会执行,它们被视为 LINQ to Entity查询。

通过添加 Select 语句,可以将 for 循环中执行的操作滚动到您的 LINQ 查询中:

public String[] ReturnPatientIDs(int CounsellingRoomID)
{
var query = this.context.CounsellingMessages
.Where(c => c.CounsellingRoomID == CounsellingRoomID)
.Select(c => c.Chatname)
.Distinct();

return query.ToArray();
}

关于c# - Entity Framework 中的 Linq 查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19484594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com