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