0

私はLINQを初めて使用するので、これは私にとっては少し難しいかもしれません。次のLINQコードがデータを適切に返します。ただし、与えられた条件は選択的です。つまり、すべての条件が一度に指定される場合と指定されない場合があります。条件はユーザー入力に基づいています。したがって、ユーザーが選択した基準に基づいて、このようなインクリメンタル LINQ を作成する方法について説明します。

        var dRows = (from TblPatientInformation in this.dsPrimaPlus1.tblPatientInformation
                    join TblDoctorMaster in this.dsPrimaPlus1.tblDoctorMaster on new { PtI_dcMId = Convert.ToInt32(TblPatientInformation.ptI_dcMId) } equals new { PtI_dcMId = TblDoctorMaster.dcM_Id }
                    join TblDepartmentMaster in this.dsPrimaPlus1.tblDepartmentMaster on new { DcM_deptMId = TblDoctorMaster.dcM_deptMId } equals new { DcM_deptMId = TblDepartmentMaster.ID }
                    join TblPatientDiagnosis in this.dsPrimaPlus1.tblPatientDiagnosis on new { PtI_Id = TblPatientInformation.ptI_Id } equals new { PtI_Id = Convert.ToInt32(TblPatientDiagnosis.ptD_ptIId) }
                    join TblDiagnosisInformation in this.dsPrimaPlus1.tblDiagnosisInformation on new { PtD_tgIId = Convert.ToInt32(TblPatientDiagnosis.ptD_tgIId) } equals new { PtD_tgIId = TblDiagnosisInformation.tgI_Id }
                    where
                      TblPatientInformation.ptI_Id > 0 ||
                      TblPatientInformation.ptI_PatientName.Contains(txtName.Text)  ||
                      TblPatientInformation.ptI_Code == int.Parse( txtCaseNo.Text) ||
                      TblDepartmentMaster.ID ==int.Parse( cmbDepartment.SelectedValue.ToString()) ||
                      TblDoctorMaster.dcM_Id == int.Parse(cmbDoctor.SelectedValue.ToString()) ||
                      TblDiagnosisInformation.tgI_Id == int.Parse(cmbDiagnosis.SelectedValue.ToString())
                    select new
                    {
                        TblPatientInformation.ptI_Id,
                        TblPatientInformation.ptI_Code,
                        TblPatientInformation.ptI_PatientName,
                        TblPatientInformation.ptI_dcMId,
                        TblPatientInformation.ptI_Age,
                        TblPatientInformation.ptI_Address,
                        TblPatientInformation.ptI_eMail,
                        TblPatientInformation.ptI_Phone1,
                        TblPatientInformation.ptI_Phone2,
                        TblPatientInformation.ptI_Phone3,
                        TblPatientInformation.ptI_Date,
                        TblPatientInformation.ptI_Gender,
                        TblDiagnosisInformation.tgI_Name,
                        TblDiagnosisInformation.tgI_Description,
                        TblDoctorMaster.dcM_FullName,
                        TblDepartmentMaster.Department
                    });
4

2 に答える 2

1

この目的のために、 Predicate Builder http://www.albahari.com/nutshell/predicatebuilder.aspxを試すことをお勧めします。

その投稿では、次のことをお勧めします。

PredicateBuilder を試す最も簡単な方法は、LINQPad を使用することです。LINQPad を使用すると、データベースまたはローカル コレクションに対して LINQ クエリを即座にテストでき、PredicateBuilder を直接サポートします (F4 を押して [PredicateBuilder を含める] をオンにします)。

これは、このアプローチを実行するための簡単な方法です。

それが役立つことを願っています。

于 2013-04-30T16:18:19.940 に答える
0

1 つの解決策は、コード式の代わりに文字列式を指定できる Dynamic LINQ を使用することです。例えば

// Dynamic Linq string expression
var result = context.People.Where("Age >= 3 And StreetNumber < 3").ToList();

とは対照的に:

// Linq code expression
var result = context.People.Where(q=>q.Age>=3 && q.StreetNumber < 3).ToList();

これにより、ユーザー入力に基づいて式を解析できます。

StringBuilder sb = new StringBuilder();
...
if(criteria1)
{
  sb.Append(" And Criteria>1");
}
...
if(criteria2)
{
  sb.Append(" And Criteria2<15");
}
...
var result = context.People.Where(sb.ToString()).ToList();

完全な例については、この Scott Gu の記事をご覧ください。

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

于 2013-04-30T16:11:53.997 に答える