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 9299767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:38:07+00:00 2026-06-18T22:38:07+00:00

I am trying to use the statement if and else in my linq query

  • 0

I am trying to use the statement if and else in my linq query but i can not show the right result ,below you can see a part of the code :

from c in dc.Train_TBLs select new {c.CodeTrain,
DBL1 = (from u in dc.Train_NormalRate_TBLs where u.CodeTrainID == c.CodeTrainID orderby u.NormalRateID ascending select u.DBL).First(),
TPL1 = (from u in dc.Train_NormalRate_TBLs where u.CodeTrainID == c.CodeTrainID orderby u.NormalRateID ascending select u.TPL == (decimal?)null ? u.DBL / 2 * 3 == (decimal?)null : u.TPL != 0).First(),....}

well in the field TPL1 my intention is to show in case the value in the “u.TPL” is equal “0” or null then will take the value from the field “u.DBL” divided for 2 and multiplicate for 3 ,but i dont get luck with my code then do you have any suggestion how to show the right result.

  • 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-06-18T22:38:09+00:00Added an answer on June 18, 2026 at 10:38 pm

    I guess this is what you want:

    select ((u.TPL ?? 0) == 0 ? u.DBL / 2 * 3 : u.TPL)
    

    Replace your last select with this.

    You said:

    in case the value in the “u.TPL” is equal “0” or null then will take the value from the field “u.DBL” divided for 2 and multiplicate for 3

    (u.TPL ?? 0) == 0 checks wheather u.TPL is 0 or null. If it is, it returns u.DBL / 2 * 3, otherwise it returns u.TPL itself. This code works only if u.TPL is nullable (defined as decimal?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to use an IF/ELSE statement to query my MySQL database, but
Im trying to use if else statement in success ajax function, but the value
Possible Duplicate: Linq If Statement I am trying to run a query using Linq,
Rather than use an if else statement, I'm trying to use the ternary operator
Trying to use a case statement to control the operation of limit. Sample query
For the code below, I am trying to use the IF statement if(mysql_num_rows($resA)>0) to
I am trying to run this query but myfield does not contain the full
I'm trying to use a CASE statement in a SQL query I have and
Here is the part of my select query that I am trying to use
i am trying to use switch case instead of If Else statement, in which

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.