tags:

views:

74

answers:

4

Hi, i am trying to get some data, but i dont know how can i do a if in linq, this is how i am trying to do

from so in db.Operations
where ((opType!= "0" ? so.Operation == int.Parse(opType) : false) 
    && (idState!=0 ? so.State == idState : false) 
    && (start != null ? so.StartDate == start : false) 
    && (end !=null ? so.EndDate == end : false))
select so

the optype is a Int, the idState is a Int, end is a datetime, start is a datime

what i am trying to do is, if those aren't null they add to the query function, so i can get all data together

for example: in c# code

if((opType!= "0")
 where (so.Operation == int.Parse(opType)
if(idState!=0)
 where (so.Operation == int.Parse(opType) && so.State == idState
.......

so if that isn't null, that sentence in that sql query (the TRUE part, i dont want to use the false part), add it to the where, so i can search all parameters that aren't null or 0

A: 
opType!= "0" ? so.Operation == int.Parse(opType) : false

you should not use so.operation == int.parse.... instead use so.operation = int.Parse(opType)

erasmus
that dont work, because i dont want to egualate .i dont know if I explaine very well, but that False, i dont want to use, i only want to use the true part
Luis
Are you saying that you should use `so.operation = int.Parse(opType)`? This would be an assignment operation and does not make sense in a where query.
Obalix
Luis
A: 

Well not sure what you want exactly but here is a try:

var query = db.Operations.AsQueryable();

if (opType != null && opType != "0")
    query = query.Where(x => x.Operation == int.Parse(opType);

if (idState != 0) 
    query = query.Where(x => x.State == idState);

if (start != null) // assuming that start is of type DateTime? otherwise use DateTime.MinValue
    query = query.Where(x => x.StartDate.Date == start); // maybe >= is more appropriate

if (end != null) // also DateTime? assumed
    query = query.Where(x => x.EndDate.Date == end); // maybe <= is more appropriate

var result = query.ToList(); // e.g. could also do an foreach, depending what you want to do

The trick in this approach is to build up the query successively. The query will not be enumerated until .ToList() or foreach is used to enumerate the list.

Actually this should yield the same:

from so in db.Operations
where ((opType != null && opType!= "0" ? so.Operation == int.Parse(opType) : true) 
    && (idState!=0 ? so.State == idState : true) 
    && (start != null ? so.StartDate.Date == start : true) 
    && (end !=null ? so.EndDate.Date == end : true))
select so
Obalix
Timing issue, my answer is similar to yours, will delete mine.
Fadrian Sudaman
No, see the edit ... the query is executed when, e.g. `.ToList()` is called. The concept used here is "deferred execution".
Obalix
ahhh thx, I'll try that :)
Luis
If you are happy you can accept the solution ... if not comment I'll try again.
Obalix
now with the firs one, its ok, i changed var to iqueryable<...> and its ok, but now, it dont show any record :S, i looking for why.
Luis
Well, without knowing your data it is not really possible to help you there, sorry. Maybe opening another question with the data, and the result you want to achieve can help you there.
Obalix
i think is the date... because in the DB have hour, how can i choose only the date in the x.Start or x.End ???i already tried x.Start.Value.ToShortDateString == start.ToShotDateString... but gives the error, operator == cannot be applied to operadord of type method and method group
Luis
Luis
See edit ... x.StartDate.Date gives you the date part.
Obalix
edit again so i can rank it :P, dont know why it cames to zero, and ask to be edit again
Luis
+1  A: 

Since you're &&'ing them, looks like you want : true instead of : false.

Tanzelax
Luis
Tanzelax
A: 

You can use conditional parameters.

Change:

where ((opType!= "0" ? so.Operation == int.Parse(opType) : false) 

To:

where ((opType!= "0" ? so.Operation == int.Parse(opType) : so.Operation == Operation) 

and so on...

JeremySpouken