2017-09-11 18 views
0

これはLINQへの私の最初の進出です。 私はまだ結果の部分の周りに私の頭を包んでいるが、私はこれから任意の結果を得るように見えることはできません。LINQクエリで結果が表示されない - WHERE句とのLEFT結合

var institutions = from lots in lotsdb.NEWinstitution 
       join webs in webbitdb.tblinstitution 
       on lots.institutionid equals webs.dispenseinstid into newinsts 
       from webs2 in newinsts.DefaultIfEmpty() 
       where webs2 == null 


       select new 
       { 
        instid = lots.institutionid, 
        instname = lots.institutionname 
       }; 

     foreach(var instfound in institutions) 
    { 

     MessageBox.Show(instfound.instid.ToString() + " " + instfound.instname.ToString()); 
    } 

私は、Visual StudioによってDATASourcesリストで作成されたデータセットを使用しています。 IsInstitutionIdNull()方法はときMSDataSetGeneratorによって生成される

var institutions = 
    from lots in lotsdb.NEWinstitution 
    join webs in webbitdb.tblinstitution on lots.institutionid equals webs.dispenseinstid 
    where webs.IsInstitutionIdNull() 

    select new 
    { 
     instid = lots.institutionid, 
     instname = lots.institutionname 
    }; 

:以下

は、私はあなたがおそらくこのようなものが必要LINQ

string strgetloc = @" 
SELECT NEWinstitution.institutionid, NEWinstitution.institutionname 
FROM NEWinstitution 
LEFT JOIN tblinstitution 
ON NEWinstitution.institutionid = tblinstitution.dispenseinstid 
WHERE (((tblinstitution.institutionid) Is Null));" 
+0

本当に空のものだけが必要ですか? 'where webs2 == null' –

+0

私はLINQの例をcanabalisingしています。これは間違っていると思います。 lotsdb.NEWinstitution.institutionidが必要です。webbitdb.tblinstitution.institutionidがNULLです –

+0

このクエリが結果を返すのは確かですか?あなたはSQLサーバーで類似した実行しようとしましたか? – gh9

答えて

0

を適応させるために「しようとした」している私の元のSQL文字列であります列はDBNullを許可します。 DBNullまたはnullと直接比較できないためです。

だから私は、次のコードを使用して終了

+0

mmmアドバイスをいただきありがとうございます。私はここで私の深さから外れていることを認めなければなりません。どのようにIsInstitutionIdNull()を動作させるのか分かりません。 –

+0

私の他のオプションは、LOTSからWEBSのtmpテーブルにデータをインポートしてから通常のSQLそれに参加してください...それは悪いことのように思えます。 –

+0

@ GlennAngel XSDを開くと、デザイナーができます。 「InstitutionId」列を「Allow null」でチェックしてください。 nullを許可すると、ジェネレータはそのメソッドを生成し、コードの背後でそれを使用できます。 –

0

を(タイプミスを固定):

var idsNotInB = dtLotsInst.AsEnumerable().Select(r => r.Field<int>("institutionid")) 
    .Except(dtWebbitInst.AsEnumerable().Select(r => r.Field<int>("institutionid"))); 

     count = idsNotInB.Count(); 

     if (count != 0) 
     { 
      DataTable dtOnlyLots = (from row in dtLotsInst.AsEnumerable() 
            join id in idsNotInB 
            on row.Field<int>("institutionid") equals id 
            select row).CopyToDataTable(); 
      using (OleDbConnection con = new OleDbConnection(PackChecker.Properties.Settings.Default["WebbitConnectionString"].ToString())) 
      { 
       string strgetloc = @"INSERT INTO tblinstitution (dispenseinstid, institutionname) VALUES (?,?)"; 

       using (OleDbCommand cmd = new OleDbCommand(strgetloc, con)) 
       { 

        con.Open(); 
        foreach (DataRow dr in dtOnlyLots.Rows) 
        { 
         cmd.Parameters.Add("?", OleDbType.Integer).Value = Convert.ToInt32(dr["institutionid"]); 
         cmd.Parameters.Add("?", OleDbType.VarWChar).Value = dr["institutionname"].ToString(); 

         cmd.ExecuteNonQuery(); 
         cmd.Parameters.Clear(); 


        } 
        con.Close(); 
       } 


      } 
     } 

これは、LINQを使用して値を見つけるために、最初のLINQセクションの「EXCEPT」を使用してうまく機能1つのテーブルではありません。 次に、このリストを使用して、必要なテーブルから行を生成します。

ありがとうございました。

関連する問題