in Education by
I'm new in LINQ so I hope this isn't a stupid question: I have a table with a lot of content presented in a datagrid, and I want the user to be able to filter the grid by using some combo boxes above the grid [like a search bar] I created a method that takes the text in the combo boxes, and placing it the "Where" clause: public void find() { string disName; string statusName; disName = RMcmbDis.Text; //This Get the first string to filter statusName = RMcmbStatus.Text; // this get the second string to filter //Here I gather all the data I need var allCNT = from x in cntDB.releases join dis in cntDB.disciplines on x.discipline equals dis.discipline_id join btch in cntDB.batches on x.batch_num equals btch.batch_id join z in cntDB.status on x.status equals z.status_id select new { dis.discipline_name, x.grade, x.batch_num, btch.batch_name, z.status_description, x.segment_leader, x.ped_leader, x.release_location, x.comments, x.QA_Verdict }; //Here I make the filtering var find = allCNT.Where(a => a.discipline_name == disName && a.status_description == statusName); dataGridView1.DataSource = find; } Now I have a problem: I want the user to be able to leave one of the combo boxes empty and if he does so, this means he doesn't want to filter that criteria. [E.G - The combo "RMcmbDis" has "Math" and the Status combo ["RMcmbStatus"] is empty, so the grid will show only "Math" in all status'. How Do I do that? Thanks guys... N. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can just add Where() clauses if the condition you want is true... var results = allCNT; if (!string.IsNullOrEmpty(disName)) results = result.Where(a => a.discipline_name == disname); if (!string.IsNullOrEmpty(statusName)) results = results.Where(a => a.status_description == statusName); dataGridView1.DataSource = results; See comment below for one option for handling lots of filters. Another option is to use a helper method: T AddFilter(IQueryable results, string filterValue, Expression> predicate) { if(!string.IsNullOrEmpty(filterValue)) return results.Where(predicate); return results; } Which you would use like this: var results = allCNT; results = AddFilter(results, disname, a => a.discipline_name == disname); results = AddFilter(results, statusName, a => a.status_description == statusName); results = AddFilter(results, whatever, a => a.whatever == whatever); // ... dataGridView1.DataSource = results;

Related questions

0 votes
    I'm new in LINQ so I hope this isn't a stupid question: I have a table with a lot of ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I'm new in LINQ so I hope this isn't a stupid question: I have a table with a lot of ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I'm new in LINQ so I hope this isn't a stupid question: I have a table with a lot of ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    Is there any way to convert following SQL statement into LINQ? select ve.EntityID , fin1.FinanceStat as ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    I am getting empty row on following sql SELECT * FROM flyers WHERE fId='6' AND 'userId'='400' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    I am trying to make a a query against a table and am kind of stuck in constructing the WHERE ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    ANSI-standard SQL allows the use of special operators in conjunction with the WHERE clause. A special ... Schemes topic in chapter Concurrency Control of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    I have this table which stores containers by region and the number of coffee pouches in each of the containers. if object_id( ... = 1 ) as sq1 ) as sq2 where sq2.LagRunningTotal...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I'm writing some very simple search functionality for a list of FAQ's. I'm splitting the search ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    The predicate in a where clause can involve Boolean operations such as and. The result of true and ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    Which oracle is the join condition is specified using the WHERE clause: (a) Oracle 9i (b) Oracle ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    I'm having an issue with the following code: private void DataPortal_Fetch(TaskCriteria criteria) { using ( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 9, 2022 in Education by JackTerrance
0 votes
    I'm having an issue with the following code: private void DataPortal_Fetch(TaskCriteria criteria) { using ( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 8, 2022 in Education by JackTerrance
0 votes
    I'm having an issue with the following code: private void DataPortal_Fetch(TaskCriteria criteria) { using ( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 8, 2022 in Education by JackTerrance
0 votes
    I'm having an issue with the following code: private void DataPortal_Fetch(TaskCriteria criteria) { using ( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 7, 2022 in Education by JackTerrance
...