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

权达 罗 1 Reputation point
2022-07-04T11:46:09.407+00:00

(
from item in _context.Table1
select new SomeModel
{
Name ="A"
}
)
.Union
(
from item in _context.Table2
select new SomeModel
{
Name = Convert.ToString(item.UserName)
}
)
it is not work,and throw out:Unable to translate set operation when matching columns on both sides have different store types.
Why?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,395 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,277 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,686 Reputation points
    2022-07-05T01:32:03.443+00:00

    the linq expression must be converted to sql. The Convert function is not supported. if using a late enough version .ToString() should work:

    Name = item.UserName.ToString()