Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

help with linq query

Tags:

linq

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

like image 502
Luis Avatar asked Feb 20 '26 16:02

Luis


1 Answers

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

like image 72
Tanzelax Avatar answered Feb 27 '26 02:02

Tanzelax