2017-07-28 43 views
0

我想聯合兩個表在一起,但其中一列是可以爲空的int,另一個是int。我嘗試將int列轉換爲可爲空的int,但得到錯誤是否有可能在linq中結合兩個不同字段類型的表?

「無效的匿名類型成員聲明器。必須使用成員賦值,簡單名稱或成員訪問權限聲明匿名類型成員。

TableA 
int? SupplierId 
string SupplierName 

TableB 
int SupplierId 
string Name 
string result; 
using (var db = Dal.MyEntities(false)) 
{ 
    result = db.TableA 
       .Select(c => new { SupplierId = c.SupplierID, SupplierName = c.SupplierName }) 
       .Union(db.TableB.Select(g => new { (int?)g.SupplierId, SupplierName = g.Name })) 
       .Where(c => c.SupplierId == supplierId) 
       .Select(c => c.SupplierName) 
       .FirstOrDefault(); 
} 

回答

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(); 
相關問題