ef 下解決 C# 中函數 Convet 到sql 不識別問題


SqlFunctions.StringConvert,。。。。。

 

Linq中字段數據類型轉換問題(Linq to entity,LINQ to Entities 不識別方法"System.String ToString()"及其餘問題 轉換 問題解決)


C#(50) arrow_triangle%20_down.jpg網絡

一、在工做中碰到這樣一個問題:ide

使用linq時,須要查詢兩個表,在這兩張表中關聯字段分別是int,和varchar()也就是string,在linq中對這兩個字段進行關聯,post

若是強制類型轉換兩個不一樣類型的字段,就會報響應的擴展方法沒法自動推斷參數類型的問題(好比:我用的是groupjoin擴展方法),this

若是進行了常規的類型轉換,好比將int字段對應的轉換爲string(ToString方法),這時編譯的時候不會有問題了。spa

可是在運行的時候會報以下錯誤:設計

LINQ to Entities 不識別方法"System.String ToString()",所以該方法沒法轉換爲存儲表達式.code

二、解決方法:orm

使用System.Data.Objects.SqlClient.SqlFunctions.StringConvert()對相應的字段進行轉換,結果能夠了。ci

好比:

我有問題的Linq是下面這個:

var borrowLeftOutJoinLog =
                query.GroupJoin(
                    groupRtnToolByID,
                    c => c.ID.ToString(),
                    d => d.BizID
, (g, f) => new { Item = g, Detail = f })
                    .SelectMany(detail => detail.Detail.DefaultIfEmpty(), (a, b) => new { a.Item, RtnSum = (decimal?)b.RtnSum })
                    .Where(a => (a.RtnSum ?? 0) < a.Item.TBor_Qty)
                    .Select(a => a.Item);

注意:上面的ID和BizID是有關聯的,可是在兩個表中分別被設計成了int,string,因此這裏對ID進行了ToString的轉換,結果出現了上面提示的錯誤。

運用解決方法後的語句以下:

var borrowLeftOutJoinLog =
                query.GroupJoin(
                    groupRtnToolByID,
                    c => System.Data.Objects.SqlClient.SqlFunctions.StringConvert((double)c.ID),
                    d => d.BizID
, (g, f) => new { Item = g, Detail = f })
                    .SelectMany(detail => detail.Detail.DefaultIfEmpty(), (a, b) => new { a.Item, RtnSum = (decimal?)b.RtnSum })
                    .Where(a => (a.RtnSum ?? 0) < a.Item.TBor_Qty)
                    .Select(a => a.Item);

三、後感:

遇到這個問題後,本身嘗試瞭解決,但是都行不通,因而轉而求助網絡(感謝這個時代吧!!!),

也看到了幾篇關於這個的解決方法,其中就包括ToString方法,並且還言之鑿鑿,再不就是說一些框架,底層問題,

難道我爲了這個芝麻大點的事,也要去本身重寫,去實現不少內容嗎??

若是是,只能說我選錯了技術,它還不成熟。

但是,結果不是這樣的,Linq出來已經好久了,它包含的內容也是不少,不該該是這樣的,因此我繼續找

在這裏我找到了:

http://stackoverflow.com/questions/1066760/problem-with-converting-int-to-string-in-linq-to-entities

這句介紹了問題的緣由:StingyJack, the problem is with the ELINQ (linq 2 entities), because it translates your code to SQL, and when it comes to an inner ToString request, it doesn't know how to translate 'ToString' to SQL. Unlike with linq 2 objects, when there is no translation, and everything is CLR lambdas, then it's performed directly on the requested objects;

下面這個給了具體的解決方法:

http://stackoverflow.com/questions/1066760/problem-with-converting-int-to-string-in-linq-to-entities/3292773#3292773

 

up vote126down voteaccepted

With EF v4 you can use SqlFunctions.StringConvert. There is no overload for int so you need to cast to a double or a decimal. Your code ends up looking like this:

var items = from c in contacts        select new ListItem
        {
            Value = SqlFunctions.StringConvert((double)c.ContactId),
            Text = c.Name
        };

share|improve this answer

edited Jul 22 '10 at 21:25

answered Jul 20 '10 at 17:44

Brian Cauthon
2,96111121


99

Why on earth wouldn't they include an overload for int? – Jeremy Coenen Oct 1 '10 at 16:59

1

What does the SqlFunctions.StringConvert part of your query look like? From the error it sounds like you are passing a Nullable<double> (double?) instead of a double. Try callingGetValueOrDefault() before you pass it in. – Brian Cauthon Apr 25 '11 at 13:23

5

@Nestor This doesn't work for SQL Compact. Found that out the hard way. – Austin Nov 8 '11 at 21:04

3

To avoid the whitespaces before the result, you should useSqlFunctions.StringConvert((double)c.ContactId).Trim() – Kim Tranjan Mar 15 '12 at 3:47

1

Seems not to work for SQLite using System.Data.SQLite The Methode 'System.String StringConvert(System.Nullable`1[System.Double])' in Typw 'System.Data.Objects.SqlClient.SqlFunctions' kann nicht in einen Speicherausdruck für 'LINQ to Entities' übersetzt werden. (cannot be translated into "LINQ to Entities") – OneWorld Jan 9 at 10:28

show 4 more comments

因此,我以爲回答別人問題時要慎重,要針對別人給出的條件本身去試驗,不能簡單憑經驗,不能說空話,更不能想固然。

給出結果要直接(固然若是要是一語中的,切中要害,那固然不用直接給出,但是我沒有這樣的功力,起碼在linq上沒有)

相關文章
相關標籤/搜索