简体   繁体   English

LINQ 到实体无法识别 System.String

[英]LINQ to Entities Not recognize System.String

I am getting this error that Linq doesn't recognize ToString .我收到Linq doesn't recognize ToString的错误。 It work without the formatting style added like the dd/MM/yyyy .它可以在没有像dd/MM/yyyy那样添加格式样式的情况下工作。 Is there a another way to do this?还有另一种方法可以做到这一点吗?

ERROR错误

LINQ to Entities does not recognize the method 'System.String ToString LINQ to Entities 无法识别方法'System.String ToString

Code代码

var MeetingList = (from m in db.Meetings
join md in db.MeetingDates on m.MeetingId equals md.MeetingId
    select new Model.Meeting
    {
        MeetingId = m.MeetingId,
        MeetingDate = md.StartTime.Value.ToString("dd/MM/yyyy")
    }).ToList();

+1 to Crowcoder's & NetMage's comment: Formatting should be a UI concern, not a domain concern unless this is an API call. +1 对 Crowcoder 和 NetMage 的评论:格式化应该是 UI 问题,而不是域问题,除非这是一个 API 调用。 (In which case ISO format date "yyyy-MM-ddTHH:mm:ssZ" would be recommended) (在这种情况下,建议使用 ISO 格式日期“yyyy-MM-ddTHH:mm:ssZ”)

To get around Linq to SQL limitations, you can opt to do a double-projection.要绕过 Linq 到 SQL 的限制,您可以选择进行双投影。 This is expanding on NetMage's example, mainly to address considerations to employ filtering and guarding against potentially null data:这是对 NetMage 示例的扩展,主要是为了解决使用过滤和防范潜在的 null 数据的注意事项:

var meetingList = db.Meetings
    // TODO: Assuming a Where filter on what Meetings to include/exclude...
    .SelectMany(m => m.MeetingDates
        .Where(md => md.StartTime.HasValue) // Avoid null ref if StartTime can be null-able
        .Select(md => new { m.MeetingId, md.StartTime }))
    .ToList()
    .Select( x => new Model.Meeting
    {
        MeetingId = x.MeetingId,
        MeetingDate = x.StartTime.Value.ToString("dd/MM/yyyy")
    }).ToList();

This assumes that you have navigation properties set up between meetings and meeting dates.这假设您在会议和会议日期之间设置了导航属性。 You can write it with explicit joins, but I highly recommend using navigation properties and letting EF work out the associations.您可以使用显式连接来编写它,但我强烈建议使用导航属性并让 EF 计算关联。

The first projection EF can convert safely to SQL and return the desired raw data.第一个投影 EF 可以安全地转换为 SQL 并返回所需的原始数据。 This is materialized with the ToList call.这通过ToList调用实现。 The second projection does the formatting against in-memory objects.第二个投影对内存中的对象进行格式化。

Double-projection should be used cautiously and avoid cases where simply slapping an extra ToList seems to fix the problem.应谨慎使用双投影,并避免简单地拍打额外的ToList似乎可以解决问题的情况。 For instance:例如:

var meetingList = db.Meetings
    // TODO: Assuming a Where filter on what Meetings to include/exclude...
    .SelectMany(m => m.MeetingDates
        .Where(md => md.StartTime.HasValue))
    .ToList() // Materializes the list to memory so the below call will work... BAD.
    .Select( md => new Model.Meeting
    {
        MeetingId = md.MeetingId,
        MeetingDate = md.StartTime.Value.ToString("dd/MM/yyyy")
    }).ToList();

This would work, however the first projection would return entire MeetingDate entities to the server even though we only want a couple columns.这会起作用,但是即使我们只需要几列,第一个投影也会将整个 MeetingDate 实体返回到服务器。 If the later Select statement were to access further navigation properties, each of these iterations would trigger a potential lazy load call back to the database.如果后面的Select语句要访问进一步的导航属性,则每次迭代都会触发潜在的延迟加载回调到数据库。 It's important when considering a double-projection that the first projection that goes to SQL is as explicit as possible in terms of what data comes back.在考虑双重投影时,重要的是,第一个投影到 SQL 就返回的数据而言尽可能明确。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ to Entities无法识别方法'System.String getFullname(System.String,System.String)' - LINQ to Entities does not recognize the method 'System.String getFullname(System.String, System.String)' LINQ to Entities 无法识别“System.String Decrypt(System.String, System.String)”方法 - LINQ to Entities does not recognize the method 'System.String Decrypt(System.String, System.String)' method LINQ to Entities无法识别方法'System.String [] ToArray [String] - LINQ to Entities does not recognize the method 'System.String[] ToArray[String] LINQ to Entities无法识别方法'System.String ToString()'方法++++++ - LINQ to Entities does not recognize the method 'System.String ToString()' method ++++++ LINQ to Entities无法识别方法'System.String - LINQ to Entities does not recognize the method 'System.String LINQ to Entities无法识别方法'System.String Format - LINQ to Entities does not recognize the method 'System.String Format LINQ to Entities无法识别方法'System.String IfNullOrWhiteSpace' - LINQ to Entities does not recognize the method 'System.String IfNullOrWhiteSpace' LINQ to Entities无法识别方法ToDateTime(System.String) - LINQ to Entities does not recognize the method ToDateTime(System.String) LINQ 实体无法识别方法'System.String ToShortDateString()'方法 - LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method LINQ to Entities无法识别方法'System.String ToString(System.String) - LINQ to Entities does not recognize the method 'System.String ToString(System.String)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM