LINQでWhere条件(だけではないが)を記述するときに、そのデータ型のプロパティやメソッドを使うことができる。
以下はそのサンプルである。
- switch (form.comboBox1.SelectedIndex)
- {
- case 0:
- var authors0 = pubs.authors.Where(a => a.state == "CA" && a.contract == true);
- bindData(form, authors0);
- break;
- case 1:
- var authors1 = pubs.authors.Where(a => a.au_fname.Length ==5 );
- bindData(form, authors1);
- break;
- case 2:
- var authors2 = pubs.authors.Where(a => a.au_fname.EndsWith("e"));
- bindData(form, authors2);
- break;
- case 3:
- var employee1 = pubs.employee.Where(e => e.hire_date >= new DateTime(1989, 1, 1) && e.hire_date <=
- new DateTime(1990, 12, 31))
- .Select(e => new
- {
- e.emp_id,
- e.fname,
- e.hire_date,
- e.job_id,
- e.job_lvl,
- e.jobs,
- e.lname,
- e.minit,
- e.pub_id
- });
- bindData2(form, employee1);
- break;
- case 4:
- var employee2 = pubs.employee.Where(e => e.hire_date.Year == 1993);
- bindData2(form, employee2);
- break;
- default:
- break;
- }
まず8行目でau_fname(string型)のLengthプロパティを使っている。
12行目では同じくstring型のEndsWithメソッド。
33行目ではhire_date(datatime型)のYearプロパティを使っている。
このようにLINQでは.NETFrameworkの機能を使ったコーディングが可能になっている。
当然、最終的にはSQLに変換されるわけなのでSQLに該当する関数がないような物については使えないのだが、基本的なものはかなりサポートされているようである。
0 件のコメント:
コメントを投稿