C# Unable to translate set operation when matching columns on both sides have different store types


運行程序時候,錯誤提示為:

Unable to translate set operation when matching columns on both sides have different store types

原因:

數據庫查下使用 Union 方法時候,前后兩個查詢的字段類型不一致導致的
例如代碼:

var list = (from d in ctx.Employee
               select new
                 {
                    d.Id,
                    AmountOfMoney = 0,
                  }).Union(from v in ctx.Product
                        select new
                         {
                            v.Id,
                            d.AmountOfMoney,
                          }
           );

錯誤解讀:
Employee表中沒有AmountOfMoney 字段,我們給定一個默認值0,系統默認是int類型
Product表中 AmountOfMoney 是decimal類型
此時編譯就會報錯了,我們給Employee 的字段加一個強制轉換類型,如下

var list = (from d in ctx.Employee
               select new
                 {
                    d.Id,
                    AmountOfMoney = (decimal)0,
                    //或者 AmountOfMoney = 0M,
                  }).Union(from v in ctx.Product
                        select new
                         {
                            v.Id,
                            d.AmountOfMoney,
                          }
           );

結果依然報錯,可能最新語法不支持這種寫法了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM