Click here to Skip to main content
15,845,830 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Dear experts ;
i'm work on c# windows application project using entity framework , sql database and i'm create winform (open form)to search about members and get result in devexpress gridcontrol put the form have many search fields can search by name , code , ip , date from to ,company , brand , model and some radio buttons i need code using linq query to find the members filter by all fields if one or more empty ignore filter and get all else select members with filter

this is a sample code but i need it for all fields type (checkbox , radio ,string ,int ,date ,lookup and combobox)

What I have tried:

string query = "select * from Members where (@name is null or MemberName = @name) and (@code is null or MemberCode = @code)";
var data = db.Members.SqlQuery(query, new SqlParameter("@name", Name), new SqlParameter("@code", code));
memberBindingSource.DataSource = data.ToList(); </pre
Posted
Updated 10-Sep-18 21:14pm
Comments
Bryian Tan 10-Sep-18 22:11pm    
what the error?

1 solution

Hello Wessam,
You can use below LINQ query
C#
var query = from d in db.Members select d;
//Filter the result based on value passed by user for Member Name
if (!string.IsNullOrEmpty(memName))
    query = query.Where(m => m.MemberName == memName);
//Filter for Member Code
if (!string.IsNullOrEmpty(memCode))
    query = query.Where(m => m.MemberCode == memCode);

//Final query execution
var memberList = query.ToList();

Happy to Help!
 
Share this answer
 
Comments
Wessam A Halim 11-Sep-18 19:33pm    
thank you very much it's work now

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900