Recently, I have worked on several projects to use the dynamic query function of linq. However, the dynamic query mechanism officially provided by Microsoft is quite complex, and many netizens have released some dynamic query solutions on the Internet, I think those solutions are complicated and difficult to understand. Now we provide a simple and practical method that is easy to understand.
The method is as follows:
Write in the middle layer:
Public List <View_InvoiceTransferLog> GetTransferLogByCondition (DateTime? StartDate,
DateTime? EndDate, int? StartNum, int? EndNum, InvoiceLogType logType)
{
Int theType = (int) logType;
IQueryable <View_InvoiceTransferLog> query = from view in Context. View_InvoiceTransferLogs
Where
(StartDate = null | view. LogDate> = startDate )&&
(EndDate = null | view. LogDate <= endDate )&&
(StartNum = null | view. InvoiceStartNum> = startNum )&&
(EndNum = null | view. InvoiceEndNum <= endNum )&&
View. TransferType = theType
Select view;
Return query. tolist ();
}
The call process is as follows:
Write at the UI Layer
Protected void searchbutton_click (Object sender, eventargs E)
{
If (! Havecondition ())
{
Getalldatabystatus ();
Return;
}
Datetime? Startdate = NULL;
Datetime? Enddate = NULL;
Int? Startnumber = NULL;
Int? Endnumber = NULL;
If (! Validatedata ())
{
Return;
}
// Get select Condition
//
If (! String. isnullorempty (startdate. Text. Trim ()))
{
Startdate = datetime. parse (startdate. Text. Trim ());
}
If (! String. IsNullOrEmpty (EndDate. Text. Trim ()))
{
EndDate = DateTime. Parse (EndDate. Text. Trim ());
}
If (! String. IsNullOrEmpty (StartNum. Text. Trim ()))
{
StartNumber = int. Parse (StartNum. Text. Trim ());
}
If (! String. IsNullOrEmpty (EndNum. Text. Trim ()))
{
EndNumber = int. Parse (EndNum. Text. Trim ());
}
List <View_InvoiceTransferLog> list = _ control. GetTransferLogByCondition (startDate, endDate, startNumber, endNumber, InvoiceLogTypeEnum );
Gd. DataSource = list;
Gd. DataBind ();
}
This guy is not on the road! Yes .!
Reprinted from: http://www.cnblogs.com/mahong/archive/2008/09/12/1289556.html