Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
986 views
in Technique[技术] by (71.8m points)

entity framework 4 - LINQ to Entities StringConvert(double)' cannot be translated to convert int to string

Problem

Need to convert int to string using EF4 + SQL CE4. The recommended option of using SqlFunctions.StringConvert(double) still gives me errors.

Option 1 (original). This gives me error:

public static IEnumerable<SelectListItem> xxGetCustomerList()
    {
        using (DatabaseEntities db = new DatabaseEntities())
        {
            var list = from l in db.Customers
                       orderby l.CompanyName
                       select new SelectListItem { Value = l.CustomerID.ToString(), Text = l.CompanyName };
            return list.ToList();
        }
    }

Option 2 (most suggested). Then as many posts suggests, I used the SqlFunctions.StringConvert() function from the library System.Data.Objects.SqlClient:

public static IEnumerable<SelectListItem> GetCustomerList()
    {
        using (DatabaseEntities db = new DatabaseEntities())
        {
            var list = from l in db.Customers
                       orderby l.CompanyName
                       select new SelectListItem { Value = SqlFunctions.StringConvert((double)l.CustomerID), Text = l.CompanyName };
            return list.ToList();
        }
    }

Which now shows below error:

The specified method 'System.String StringConvert(System.Nullable`1[System.Double])' on the type 'System.Data.Objects.SqlClient.SqlFunctions' cannot be translated into a LINQ to Entities store expression.

Option 3 (for very specific case). Then anoter post shows a smart solution using Dictionary, which finally works:

public static IEnumerable<SelectListItem> xGetCustomerList()
    {
        using (DatabaseEntities db = new DatabaseEntities())
        {
            var customers = db.Customers.ToDictionary(k => k.CustomerID, k => k.CompanyName);
            var list = from l in customers
                       orderby l.Value
                       select new SelectListItem { Value = l.Key.ToString(), Text = l.Value };
            return list.ToList();
        }
    }

But only work for simple pair values (key, value). Can someone help me with another solution or what I'm doing wrong with option 2?

And I hope Microsoft will soon make EF right before pushing us to move from L2S which is already stable and much more mature. I actually using EF4 just because want to use SQL CE, otherwise I stay with L2S.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EF is database independent at upper layers but the part dealing with conversion of linq query to SQL is always database dependent and SqlFunctions are dependent on SQL Server Provider but you are using SQL Server CE provider which is not able to translate functions from SqlFunctions class.

Btw. third option is also not a solution because it will select whole customer table to memory and use linq-to-objects after that. You should use this:

public static IEnumerable<SelectListItem> xxGetCustomerList()
{
    using (DatabaseEntities db = new DatabaseEntities())
    {
        // Linq to entities query
        var query = from l in db.Customers
                    orderby l.CompanyName
                    select new { l.CustomerID, l.CompanyName };

        // Result of linq to entities transformed by linq to objects
        return query.AsEnumerable()
                    .Select(x => new SelectListItem
                        {
                           Value = x.CustomerID.ToString(),
                           Test = x.CompanyName  
                        }).ToList();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...