1

CompanyID と TestCompanyID を持つテーブル名 TestNotifications があります。これらの ID は、companyName 列を持つ Companies テーブルにリンクしています。会社と testCompany の companyName を取得する必要があります。以下のコードが機能しません。暗黙的に変換できないというエラーが発生します。どんな助けでも感謝します。

testNotifications = from t in db.CT_TestNotifications
    join c in db.CT_Companies on t.CompanyID equals c.CompanyID
    join tc in db.CT_Companies on t.TestCompanyID equals tc.CompanyID 
    select new
    {
        t.TestNotificationID,
        c.CompanyName,
        //tc.CompanyName
        TestCompanyName = tc.CompanyName
    };

エラーは次のとおりです。

Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to
'System.Linq.IQueryable<CT_TestNotification>'. An explicit conversion exists 
(are you missing a cast?)
4

3 に答える 3

3

匿名型に射影していますが、testNotifications期待していCT_TestNotificationます。

のインスタンスを作成してみてCT_TestNotificationくださいselect:

testNotifications = from t in db.CT_TestNotifications
    join c in db.CT_Companies on t.CompanyID equals c.CompanyID
    join tc in db.CT_Companies on t.TestCompanyID equals tc.CompanyID
    select new CT_TestNotification       // Here's the major difference
    {
        PropName = t.TestNotificationID, // PropName must be changed to the
        PropName = c.CompanyName,        // properties of your actual class
        //tc.CompanyName
        TestCompanyName = tc.CompanyName
    };
于 2012-06-13T16:22:08.177 に答える
0

条件の結合が同じタイプであるかどうかを確認します-IEはt.CompanyIDintおよびc.CompanyIDです。

于 2012-06-13T16:18:21.063 に答える
0

問題は、testNotifications の宣言方法にあると思われます。次のようなことをすると、そのエラーが表示されます。

IQueryable<CT_TestNotifications> testNotifications;

testNotifications = from t in db.CT_TestNotifications 
    join c in db.CT_Companies on t.CompanyID equals c.CompanyID 
    join tc in db.CT_Companies on t.TestCompanyID equals tc.CompanyID  
    select new 
    { 
        t.TestNotificationID, 
        c.CompanyName, 
        //tc.CompanyName 
        TestCompanyName = tc.CompanyName 
    }; 

値の型を既知の型から匿名型に変更しようとしているために、最初に testNotifications を宣言し、後でクエリを続行した場合にも、この同じ問題が発生します。

var testNotifications = db.CT_TestNotifications;
    testNotifications = from t in testNotifications 
        join c in db.CT_Companies on t.CompanyID equals c.CompanyID 
        join tc in db.CT_Companies on t.TestCompanyID equals tc.CompanyID  
        select new 
        { 
            t.TestNotificationID, 
            c.CompanyName, 
            //tc.CompanyName 
            TestCompanyName = tc.CompanyName 
        }; 

匿名型を投影しているため、型推論を使用する必要があり、変数を明示的に宣言しないでください。

var testNotifications = from .. select ..;
于 2012-06-13T17:33:59.510 に答える