c# - Sql Multiple Conditions Method -


i've been trying find way of using multiple conditions sql, @ moment using entity framework. there chance way achieve want wondered if knew more efficient method.

essentially using criteria find next row in database should picked. example lets use 2 following conditions:

1) field1 = a;

2) field2 = b;

so in following table:

| rowid | field1 | field2 |  | 0001  |         |  b |  | 0002   | b | b |  | 0003 | c | c |  | 0004 | | c | 

i need pick each row individually in following order:

0001 - both condtions satisfied,  0004 - condition 1 satisfied,  0002 - condition 2 satisfied,  0003 - no conditions satisfied 

at moment doing following

public testobj getnextobj() {     using (testdb testdb = new testdb())     {         testobj testobj = (from o in testdb.testtable                            o.field1 == && o.field2 == b                            select o).firstordefault();          if (testobj != null)            return testobj;          testobj = (from o in testdb.testtable                            o.field1 ==                            select o).firstordefault();          if (testobj != null)            return testobj;          testobj = (from o in testdb.testtable                            o.field2 == b                            select o).firstordefault();          if (testobj != null)            return testobj;          testobj = (from o in testdb.testtable                            select o).firstordefault();           return testobj;     } } 

this works okay, want allow conditions defined in table , worried when number of conditions increases process begin taking long time.

is there way attempting here??

thanks.

edit:::::

now using following code select items table in order defined table::

public static sorttest getrow()     {         using (testdb testdb = new testdb())         {             sortparam[] sortparams = (from sp in testdb.sortparams                                       orderby sp.priority ascending                                       select sp).toarray();              if (sortparams.length == 0)             {                 sorttest sorttest = (from st in testdb.sorttests                                      orderby st.rowid ascending                                       select st).firstordefault();                 console.writeline("short route");                 return sorttest;             }              console.writeline("long route");              stringbuilder sqlquerybuilder = new stringbuilder();              sqlquerybuilder.append("select * [proto].[dbo].[sorttests] order \n");              foreach (sortparam sortparam in sortparams)             {                 sqlquerybuilder.append("case when " + sortparam.fieldname + " '%" + sortparam.fieldvalue + "%' 1 else 2 end,\n");             }              sqlquerybuilder.append("\nrowid"); //by default use row id             dbsqlquery<sorttest> dbsqlquery = testdb.sorttests.sqlquery(sqlquerybuilder.tostring());             return dbsqlquery.firstordefault();         }     } 

i may have alter thigns prevent sql injection, works now.

thanks!

there's simple way of doing in single query in sql:

select * o order case field1 when 'a' 1 else 2 end,          case field2 when 'b' 1 else 2 end,          rowid 

Comments