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

When running the program, the error prompt is:

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

reason:

When the union method is used for database query, the field types of the two queries are inconsistent, resulting in

Example code:

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

Wrong interpretation:

There is no accountofmoney field in the employee table. We give a default value of 0. The system defaults to int type

Amountofmoney in the product table is the decimal type

At this time, the compilation will report an error. We add a cast type to the field of employee, as follows

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

The result is still an error. Maybe the latest grammar doesn’t support this writing method

Similar Posts: