Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

c# object filter and change order

Posted on 2013-11-11
6
Medium Priority
?
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 45

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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

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 64

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 45

Accepted Solution

by:
AndyAinscow earned 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

596 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