Link to home
Start Free TrialLog in
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

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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?
Avatar of Anthony Matovu

ASKER

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
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial