LINQ to Entities无法识别方法String.Format

时间:2016-05-03 12:54:01

标签: c# string entity-framework linq formatting

我正在尝试格式化双值(仅显示2位小数)。我试图使用AsEnumerable但我不断收到此错误

  

LINQ to Entities无法识别该方法   的String.Format

var tw = workers.Select(x => new
            {
                Id = x.Id,
                JobOpportunityFeedbacks = x.JobOpportunityFeedbacks.AsEnumerable().
                Select(y => new
                {
                    Rating = String.Format("0.00",y.Rating),
                    Feedback = y.Feedback
                });

2 个答案:

答案 0 :(得分:1)

您必须在初始AsEnumerable

之外执行Select
var tw = workers.Select(x => new
    {
        Id = x.Id,
        JobOpportunityFeedbacks = x.JobOpportunityFeedbacks
            .Select(y => new
            {
                y.Rating,
                y.Feedback
            })
    })
    .AsEnumerable()
    .Select(x => new 
    {
        x.Id,
        JopOpertunityFeedbacks = x.JobOpportunityFeedbacks
            .Select(y => new
            {
                Rating = String.Format("0.00",y.Rating),
                y.Feedback
            })
    });

答案 1 :(得分:0)

使用SqlFunctions类 - 我没试过,但应该可以工作。

var tw = workers.Select(x => new
            {
                Id = x.Id,
                JobOpportunityFeedbacks = x.JobOpportunityFeedbacks.AsEnumerable().
                Select(y => new
                {
                    Rating = SqlFunctions.StringConvert(y.Rating, 4, 2)
                    Feedback = y.Feedback
                });

https://msdn.microsoft.com/en-us/library/dd487158(v=vs.110).aspx