tags:

views:

77

answers:

4

I am using LINQ to write a query - one query shows all active customers , and another shows all active as well as inactive customers.

 if(showall)
 {
   var prod = Dataclass.Customers.Where(multiple factors ) (all inactive + active)
 }
 else
 {
   var prod = Dataclass.Customers.Where(multiple factors & active=true) (only active)
 }

Can I do this using only one query? The issue is that, multiple factors are repeated in both the queries

thanks

A: 

Wouldn't you just use your first query to return all customers?? If not you'd be returning the active users twice.

Fermin
actually I also have if\else condition, and only one query is fired.
junky_user
"and only one query is fired" - so why are you asking this question
BlueRaja - Danny Pflughoeft
I think you might need to update your question so that you include things like this if/else condition.
Fermin
+1  A: 

I'm assuming you are trying to minimze the number of roundtrips?

If "multiple factors" is the same, you can just filter for active users after your first query:

var onlyActive = prod.Where(p => p.active == true);
ericvg
I'd replace your expression with `prod.Where(p => p.active)`
Mehrdad Afshari
+3  A: 
var customers = Dataclass.Customers.Where(multiple factors);
var activeCust = customers.Where(x => x.active);

I really don't understand the question either. I wouldn't want to make this a one-liner because it would make the code unreadable

masenkablast
thanks, this worked
junky_user
A: 

Options I'd consider

  • Bring all customers once, order by 'status' column so you can easily split them into two sets
  • Focus on minimizing DB roundtrips. Whatever you do in the front end costs an order of magnitude less than going to the DB.
  • Revise user requirements. For ex. consider paging on results - it's unlikely that end user will need all customers at once.
Ariel
I would like add to the OP's point about DB costs. In many cases (if not most), the extra overhead incurred by the roundtrip to the database is NOT so much the database taking a long time, but the overhead time to make the request and to transfer the results back to the client. It's just like making a webservice call costs more than calling a local method.
Matthew Wood
True. Rule of thumb, for every physical layer, multiply by 10, if not 100 in some cases.
Ariel