It's awesome to query the dynamic data in LINQ!

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.