Avatar of Anthony Matovu
Anthony MatovuFlag for Uganda asked on

putting or condition in a linq

I have built a linq query as below and so far i think it is fine,

Please advise me if i could built better, i am extremely new to linq queries,

secondary: how do i improve the second last line

  .mou_onn_tot_bno = grp.Sum(Function(x) If(x.Field(Of String)("utype") = "UG", x.Field(Of Decimal)("cdur"), 0)),

to include the condition (lower(wallet_type_cd) = 'loyalty'" & " or lower(wallet_type_cd)='bonus' or lower(wallet_type_cd)='promo')

I have managed with the and operator in the last line (advise me if i could do it better) but failing with the or operator


   Dim tbl_time As DataTable = ds.Tables("ctime")
        Dim tbl_perhour As DataTable = ds.Tables("perhour")
        Dim tbl_area As DataTable = ds.Tables("areas")
        Dim tbl_basestation As DataTable = ds.Tables("station")
        Dim tbl_country As DataTable = ds.Tables("country")

        TextBox1.Text = Now
        TextBox1.Refresh()


        Dim query = From obj_perhour In tbl_perhour.AsEnumerable() Join obj_time In tbl_time.AsEnumerable() _
                    On obj_perhour.Field(Of Integer)("time_key") Equals obj_time.Field(Of Integer)("time_key") _
                    Join obj_area In tbl_area.AsEnumerable() _
                    On obj_perhour.Field(Of Integer)("wallet_cd") Equals obj_area.Field(Of Integer)("wallet_cd") _
                    Join obj_basestation In tbl_basestation.AsEnumerable() _
                    On obj_perhour.Field(Of Integer)("base_station_key") Equals obj_basestation.Field(Of Integer)("base_station_key") _
                    Join obj_country In tbl_country.AsEnumerable() _
                    On obj_perhour.Field(Of Integer)("number_prefix_key") Equals obj_country.Field(Of Integer)("number_prefix_key")
                    Group obj_perhour By customerid = obj_perhour.Field(Of Integer)("msisdn"), _
                    time_hour = obj_perhour.Field(Of DateTime)("time_hour"), _
                    cellid = obj_perhour.Field(Of DateTime)("cellid"), _
                    sitename = obj_perhour.Field(Of DateTime)("sitename") _
                    Into grp = Group _
                       Select New With _
                            { _
                              .msisdn = customerid, .time_hour = time_hour, .cellid = cellid, .sitename = sitename, _
                              .rev_onn_tot_nbn = grp.Sum(Function(x) If(x.Field(Of String)("utype") = "UG", x.Field(Of Decimal)("ccost"), 0)),
                              .mou_onn_tot_nbn = grp.Sum(Function(x) If(x.Field(Of String)("utype") = "UG", x.Field(Of Decimal)("cdur"), 0)),
                               .mou_onn_tot_bno = grp.Sum(Function(x) If(x.Field(Of String)("utype") = "UG", x.Field(Of Decimal)("cdur"), 0)),
                              .rev_onn_voi_nbn = grp.Sum(Function(x) If(x.Field(Of Int16)("utype") = 44, If(x.Field(Of String)("utype") = "UG", x.Field(Of Decimal)("ccost"), 0), 0))}

Open in new window

.NET ProgrammingASP.NETVisual Basic.NET

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon
Fernando Soto

To your question, "Please advise me if i could built better, i am extremely new to linq queries,", in my opinion the query is good as it is and no need to change it.

To this part of the question, "secondary: how do i improve the second last line", I am not understanding what you mean by this, "to include the condition (lower(wallet_type_cd) = 'loyalty'" & " or lower(wallet_type_cd)='bonus' or lower(wallet_type_cd)='promo')",?

What is lower, is it a function outside of the query? and if it is  loyalty, bonus and / or promo how are you wanting to sum them up?
ASKER
Anthony Matovu

Sorry Fernando AND EXPERTS, i just carried the code from another program and i wanted to implement it in VB.NET linq.

I would want to change the code :
 .mou_onn_tot_bno = grp.Sum(Function(x) If(x.Field(Of String)("utype") = "UG", x.Field(Of Decimal)("cdur"), 0)),

to sum only when utype = 'UG', AND (wallet_type_cd = 'loyalty'"  or wallet_type_cd='bonus' wallet_type_cd='promo')

IN SHORT I AM FAILING TO IMPLEMENT THE OR IN THE IF CONDITION.

THANK YOU
ASKER CERTIFIED SOLUTION
Fernando Soto

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes