Solved

c# object filter and change order

Posted on 2013-11-11
6
349 Views
Last Modified: 2013-11-14
I have the following employee object.

Emp_Number            Name          Designation      salary
1                                   aa               Employee         30000
2                                   a1               Manager           60000
3                                    b1              Employee         35000
6                                   b2              Employee          32000
5                                   c1               Manager           40000



This is the code to get the details.

 public List<Employee> GetEmployeeDetailsAllDesc()
        {
            List<Employee> employeeDetailAllDesc = new List<Employee>();

            string connectionString = GetConnection();
            string queryString = "SELECT * "
              + " FROM employee  order by Emp_Number desc";
            using (SqlConnection connection = new SqlConnection(
                       connectionString))
            {
                SqlCommand command = new SqlCommand(
                    queryString, connection);
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();

                if (reader == null || !reader.HasRows)
                {
                    employeeDetailAllDesc = null;
                    return employeeDetailAllDesc ;
                }

                try
                {
                    while (reader.Read())
                    {

                        Employee   employeeAllDesc = new Employee   ();
                        employeeAllDesc.Emp_Number = (int)reader["Emp_Number"];
                        employeeAllDesc.Name = reader["Name"].ToString();
                        employeeAllDesc.Designation= reader["Designation"].ToString();
                        employeeAllDesc.Salary= reader["Salary"];
                        employeeDetailAllDesc.Add(employeeAllDesc);
                    }
                }
                finally
                {
                              reader.Close();
                }
            }

            return  employeeDetailAllDesc
        }




I need to get the 3 different types of result from this  object.

Result 1:  Employee Detail order by Emp_Number    Ascending order

Result 2:  Employee Detail order by Emp_Number    Descending order

Result 3:  Employee Detail  Designation ='Employee'

I do not want to use the three different method to get the  above details and it will hit the DB three times. So how can I achieve this in single hit ?
0
Comment
Question by:Varshini S
  • 2
  • 2
  • 2
6 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39638968
string queryString;
if(condition1)
queryString = "SELECT * " + " FROM employee  order by Emp_Number desc";
else
 {
  if(condition2)
   queryString = ...
  else
    queryString = ...
 }

Just put in what you require as the conditions and the different (order by clauses) SQL statements and then your function can cope with all three cases you require.
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 39639054
Hi;

You can send all three commands to SQL server and return all three result sets in one call. Make your queryString as follows:

 string queryString = 
       "SELECT * FROM employee ORDER BY Emp_Number ASC;"
     + "SELECT * FROM employee ORDER BY Emp_Number DESC;" 
     + "SELECT * FROM employee WHERE Designation = 'Employee'";

Open in new window

   

Then process the results of the data reader as normal in your code. When you reach the end of the records in the data reader then you can execute the following command on the reader to get to the next result set.

reader.NextResult();

Open in new window


Which will place you just before the first record in the second result set. Then do the same for the last result set.
0
 

Author Comment

by:Varshini S
ID: 39639254
Is it possible to this operation in object level ?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Varshini S
ID: 39639261
FernandoSoto- Instead of doing in SQL server can I do it on the object level using linq ?
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39639343
Sure it can be but that means that you will need to bring the complete employee table down to local system first.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 250 total points
ID: 39640825
>>Is it possible to this operation in object level ?

Yes, no problems.  The initial code you showed brings all the records back (you didn't use any filter, just a sort).  So you can re-order the list without having to requery the database.  I would use a custom sort function but LINQ should do the job as well.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question