Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi I need to find data in a table by one or all properties, my idea is to fill an entity and make a search by all properties if the parameter is not null

I have 2 ideas for this but i dont know if is posible for both cases.

If there is a better way im all ears too :P

Thanks and regards

First set an entity and use it to search.

C#
public bool find(nullable<int>param1,nullable<string>param2,nullable<string>param3)
{

Entity e = new Entity(); 

e.prop1 = param1;
e.prop2 = param2;
e.prop3 = param3;

//here is what i dont know if is posible
var results = dbContext.....(e);

}



or my second idea was to ask if parameter is not null to add it to the query

C#
public bool find(nullable<int>param1,nullable<string>param2,nullable<string>param3)
{

//here is what i dont know if is posible
var results = dbContext.where(
                         if (param1!=null) e=> e.prop1 == param1, 
                         if (param2!=null) e=> e.prop2 == param2,
                         if (param3!=null) e=> e.prop3 == param3);

}
Posted
Updated 1-Sep-13 4:53am
v2
Comments
Sergey Alexandrovich Kryukov 1-Sep-13 11:55am    
Why using nullable for a string?! it is already nullable, as it is a reference type.
—SA

1 solution

 
Share this answer
 

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