Solved

c# object filter and change order

Posted on 2013-11-11
6
344 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
Comment Utility
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
Comment Utility
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
Comment Utility
Is it possible to this operation in object level ?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:Varshini S
Comment Utility
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
Comment Utility
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
Comment Utility
>>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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now