是否可以在linq中将两个具有不同字段类型的表联合起来?

时间:2017-07-28 18:05:00

标签: c# linq

我正在尝试将两个表结合在一起,但其中一个列是可以为空的int,另一个是int。我尝试将int列转换为可以为null的int但是得到错误

  

"无效的匿名类型成员声明符。必须使用成员分配,简单名称或成员访问来声明匿名类型成员。"

img {
    width: 100px; // how much you want
    min-height: 100px;
    float: left;
    margin: 10px;
}
TableA
int? SupplierId
string SupplierName

TableB
int SupplierId
string Name

1 个答案:

答案 0 :(得分:3)

来自MSDN

  

您必须为正在初始化的属性提供名称   一个表达

在您的情况下,您应该为要投射到Nullable int type

的表达式提供属性
    result = db.TableA
                .Select(c => new { SupplierId = c.SupplierID, SupplierName = c.SupplierName })
                .Union(db.TableB.Select(g => new { SupplierId = (int?)g.SupplierId, SupplierName = g.Name }))
                .Where(c => c.SupplierId == supplierId)
                .Select(c => c.SupplierName)
                .FirstOrDefault();
相关问题