【问题标题】:LINQ to Entities - method cannot be translated into a store expressionLINQ to Entities - 方法无法转换为存储表达式
【发布时间】:2017-10-28 21:23:32
【问题描述】:

相同的查询在 .Net 3.5 中有效,但在 .Net 4.5.2 中无效 这里有很多帖子都有同样的错误,几乎都试过了,但没有用。 我已将所有内容提取到一个单独的变量中进行查询。我仍然收到错误 -

LINQ to Entities 无法识别方法“System.String Format(System.String, System.Object)”方法,并且该方法无法转换为存储表达式。

 private void LoadAppointmentData()
        {
            var user = Session["user"].ToString();
            var userFirstName = db.Users.SingleOrDefault(u => u.FirstName == user);

            var userFN = userFirstName.username;
            var chwWorker = from c in db.PatientContacts
                            where c.UserName == userFN &&
                                 (c.PCP_Status == "Appointment Made" || c.PCP_Status_AWC == "Appointment Made"
                                   || c.PCP_Status_AWDV == "Appointment Made" ||
                                 (c.PCP_Status == "RX for Mamogram" && c.Status == "Appointment Made")) 
                            orderby c.PCP_Status_Date descending 
                            select new
                            {
                                Id = c.MemberID,
                                Name = c.PatientFirstName + " " + c.PatientLastName, 
                                PCP_Appt = $"{c.PCP_Status_Date:d}",
                                Mammogram_Appt = $"{c.StatusDate:d}",
                                Phone = GetPhone(c.MemberID)
                            };
            if (chwWorker.Any())
            {
                if (grvAppointmentList != null)
                {
                    pnlAppointmentFollowUp.Visible = true;
                    grvAppointmentList.DataSource = chwWorker;
                    grvAppointmentList.DataBind();
                }
            }
}

我不确定要运行此查询还需要进行哪些更改。

【问题讨论】:

  • $"{c.PCP_Status_Date:d}"is String.Format。我假设您知道错误的含义。
  • 但在早期版本中也是如此......
  • 不,string.Format 从来不是实体框架中支持的方法。我假设您在 .Net 3.5 中使用了 LINQ-to-SQL,它会自动切换到客户端评估 LINQ 查询中无法转换为 SQL 的部分。
  • 我尝试转换为 PCP_Appt = string.Format("{0:d}", c.PCP_Status_Date),但仍然收到相同的错误 - “消息”LINQ to Entities 无法识别该方法' System.String Format(System.String, System.Object)' 方法,并且该方法不能翻译成存储表达式。" s
  • @GertArnold,是的,你是对的,我在 .Net 3.5 中使用了 LINQ-to-SQL,

标签: c# asp.net entity-framework linq


【解决方案1】:

在使用Select之前,您需要使用“LINQ to Objects”来执行string.Format或使用AsEnumerable()ToList()的插值字符串:

var chwWorker = (from c in db.PatientContacts
                where c.UserName == userFN &&
                     (c.PCP_Status == "Appointment Made" || c.PCP_Status_AWC == "Appointment Made"
                      || c.PCP_Status_AWDV == "Appointment Made" ||
                     (c.PCP_Status == "RX for Mamogram" && c.Status == "Appointment Made")) 
                orderby c.PCP_Status_Date descending select c) 
                .AsEnumerable() // or 'ToList()'
                .Select(c => new 
                {
                     Id = c.MemberID,
                     Name = c.PatientFirstName + " " + c.PatientLastName, 
                     PCP_Appt = $"{c.PCP_Status_Date:d}",
                     Mammogram_Appt = $"{c.StatusDate:d}",
                     Phone = GetPhone(c.MemberID)
                });

请注意,string.Format 方法无法被 LINQ to Entities 识别为将其转换为 SQL 命令,因此需要将查询结果具体化到内存中。

注意:如果您在使用 Any() 方法之前仍希望 LINQ to Entities 查询,则可以使用 SqlFunctions.StringConvert,但并非所有 SQL 提供程序都能够将其转换为 SQL 语句。

相关问题:

LINQ to Entities does not recognize the method 'System.String Format

【讨论】:

  • 感谢@Tetsuya Yamamoto - 我必须使用 Select 运算符才能使查询像这样工作“... orderby c.PCP_Status_Date descending select c) .AsEnumerable()...”并且它可以工作现在好了。否则,它会抛出“查询正文必须以 select 子句或 group 子句结尾。感谢您帮助解决这个问题。
猜你喜欢
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
  • 1970-01-01
  • 2015-04-02
  • 1970-01-01
相关资源
最近更新 更多