Reputation: 1068
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.
Upvotes: 0
Views: 130
Reputation: 18443
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?
)
Upvotes: 3