2017-07-28 13 views
0

私は2つの表を一緒に結合しようとしていますが、列の1つはNULL可能intであり、もう1つはintです。私はNULL可能intにint型の列をキャストしようとしたが、エラーにlinqで異なるフィールドタイプの2つのテーブルを結合することは可能ですか?

を得る「無効な匿名型のメンバ宣言子。匿名型メンバーは、メンバーの割り当て、単純名またはメンバのアクセスを宣言する必要があります。」 MSDNから

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

あなたは、あなたのケースでは 表現

で初期化されているプロパティの名前を提供する必要があります表現の自由を提供するべきあなたがキャストしているイオン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(); 
関連する問題