Whereで複数条件を指定した場合の記述(Linq To Entities )
今回は、Where区で複数条件を指定してデータを取得する方法です。
Dim con As New DB Dim getdata as AAA () = con.AAA.Where(Function(x) x.ID = CType(txtID.Text, Integer) And _ x.部署 = CType(txtBuCd.Text, Integer)).ToArray
ただし、Option Strictが「ON」の場合は上記の書き方だと波線が表示されコンパイルがうまくいかない。
その場合は、下記のようにして対応した。
んー、微妙な感じですけど。。。。良い方法ないですかね。。。
Dim con As New DB Dim getdata as AAA () = con.AAA.Where(Function(x) Ctype(x.ID = CType(txtID.Text, Integer) And _ x.部署 = CType(txtBuCd.Text, Integer), Boolean)).ToArray
上記のデータを取り出したいときは下記のようにして取り出せます。
if getdata.Count > 0 then txtNm.text = getdata(0).名前 txtAg.text = getdata(0).年齢 end if
※1件だけを出力したい時とかに使えると思います。
以上!