Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 660113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:07:13+00:00 2026-05-13T23:07:13+00:00

i am trying to get some data, but i dont know how can i

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:07:13+00:00Added an answer on May 13, 2026 at 11:07 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get some XML data with LINQ, but running into a
Like some of you allready know, i'm trying to get data from facebook in
I am trying to get some xpath from xsl variable using xsl ver 1.0
I'm trying to get an insert to work using LINQ and am having some
I have a website where an ajax call will get some Json data from
I am trying to parse some very basic JSON, but I don't know where
I need to make a mysql connection and get some data back. I can
I'm trying to get the css value of the background-image of some div, but
I'm receiving some 'body' content from a jquery's json call, where I can get
I am trying to get some accurate runtime comparisons of PHP vs Python (and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.