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,
                          }
           );

结果依然报错,可能最新语法不支持这种写法了

posted @ 2021-11-13 11:56  清和时光  阅读(1309)  评论(2编辑  收藏  举报