2017-09-17 145 views
-1

我只是嘗試使用與EFF與Mysql以下Linq查詢導出[]數組裏面:如何爲int轉換爲字符串LINQ查詢

DM_ItemGroup[] array = 
    (from item_grp in DbContext.hexa_item_group 
    join item_btn in DbContext.hexa_button 
      on item_grp.GroupID equals item_btn.ButtonID 
    where item_btn.ButtonType.Equals("G", StringComparison.Ordinal) 
    select new DM_ItemGroup 
       { 
       GroupID = SqlFunctions.StringConvert((decimal)item_grp.GroupID), 
       GroupName = item_grp.GroupName, 
       ButtonID = item_btn.ButtonID, 
       Default_TaxId = item_grp.Default_TaxId, 
       Out_Of_Sales = item_grp.Out_Of_Sales, 
       Sales_Seq = item_grp.Sales_Seq, 
       DataModel_Button = new DM_Button(), 
       }).ToArray<DM_ItemGroup>(); 

我最初嘗試.ToString(),但它給了一個例外。試圖SqlFunctions.StringConvert後,我收到以下錯誤: -

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

如何將羣ID(這是詮釋我的表)爲String(這是由我的DataModel需要)轉換?

+1

的可能的複製[LINQ到實體StringConvert(雙)​​」不能轉換爲INT轉換爲字符串(https://開頭計算器.com/questions/5771299/linq-to-entities-stringconvertdouble-can-be-translate-to-convert-int-to-s) – CodeCaster

+0

@CodeCaster:我已經看過所有這些建議,並且實現了相同的功能,但沒有爲我工作。因此,請給我一個解決方案。 –

+0

哦,是的,我錯過了「MySQL」的一部分。你發現SqlFunctions是特定於SQL Server的,對嗎? – CodeCaster

回答

0

感謝您投下一個完全合法的問題。如果有人以平等的速度回答了這個問題。

我找到了解決我的問題,就是如下: -

using (HexaEntities hh=new HexaEntities()) 
      { 
       var cc = hh.hexa_item_group.ToDictionary(k => k.GroupID, k => k); 
       var lst = from l in cc 
          orderby l.Key 
          select new DM_ItemGroup {GroupID = l.Key.ToString(), GroupName = l.Value.GroupName, Default_TaxId=l.Value.Default_TaxId,ButtonID=l.Value.ButtonID.Value,Out_Of_Sales=l.Value.Out_Of_Sales,Sales_Seq=l.Value.Sales_Seq }; 
       AllRecords = lst.ToList<DM_ItemGroup>(); 

      }