C# 是否可以在linq中合并两个具有不同字段类型的表?
我尝试将两个表合并在一起,但其中一列是可为null的int,另一列是int。我尝试将int列强制转换为可为null的int,但出现了错误 无效的匿名类型成员声明符。匿名类型成员必须使用成员分配、简单名称或成员访问权限声明 发件人: 必须为正在初始化的属性提供名称 表情 在您的情况下,您应该为要强制转换为C# 是否可以在linq中合并两个具有不同字段类型的表?,c#,linq,C#,Linq,我尝试将两个表合并在一起,但其中一列是可为null的int,另一列是int。我尝试将int列强制转换为可为null的int,但出现了错误 无效的匿名类型成员声明符。匿名类型成员必须使用成员分配、简单名称或成员访问权限声明 发件人: 必须为正在初始化的属性提供名称 表情 在您的情况下,您应该为要强制转换为Nullable int type的表达式提供属性 result = db.TableA .Select(c => new { SupplierId
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();
发件人:
必须为正在初始化的属性提供名称
表情
在您的情况下,您应该为要强制转换为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();
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();