I am having 5 controls on my web page like txtFirstName
(TextBox), txtDisplayName
(TextBox), txtFromDate
(TextBox), txtToDate
(TextBox) and btnFilter
(button). Also I am binding some data on GridView same page by using DataTable.
Now I want to filter the DataTable by using these search parameters controls which I have mentioned in the above.
Filter Condition is,
We are not sure about the filled controls and empty controls. So based on the input entered on the controls we need to filter the DataTable.If the control values are empty we should not allow them to filter Datatable.
We need to filter the data values by LIKE operator or String.Startswith()
If txtFromDate and txtToDate have filled then we need to filter the data values between the range.
If all controls are filled then we need to filter the datatable based on all filter parameters wit the date range.
How do we achieve this filtering? Please help me to implement this successfully. Can we do this by LINQ or Lamda expression? If so, tell me the suggestions.
I have tried two ways but its not working ,
First way:
var test=dtActions.AsEnumerable().Where(z=>
!string.IsNullOrEmpty(txtFirstName.Text)? z.Field<string>("FirstName").ToUpper().StartsWith(txtFirstName.Text.ToUpper()) &&
!string.IsNullOrEmpty(txtDisplayName.Text)?z.Field<string>("DisplayName").ToUpper().StartsWith(txtDisplayName.Text.ToUpper()) &&
!string.IsNullOrEmpty(txtCreatedBy.Text)?z.Field<string>("CreatedBy").ToUpper().StartsWith(txtCreatedBy.Text.ToUpper());
I dont have idea to get the data between the DateTime textboxes.
Second way:
var results = from dataRow in dtActions.AsEnumerable()
where dataRow.Field<string>("FirstName").ToUpper().StartsWith(txtFirstName.Text.ToUpper()) ||
dataRow.Field<string>("DisplayName").ToUpper().StartsWith(txtFirstName.Text.ToUpper()) ||
((dataRow.Field<DateTime>("CreatedDate")>= Convert.ToDateTime(txtFromDate.Text)) && (dataRow.Field<DateTime>("CreatedDate")<= Convert.ToDateTime(txtToDate.Text)))
select dataRow;