We help IT Professionals succeed at work.

putting or condition in a linq

54 Views
Last Modified: 2016-02-02
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

Comment
Watch Question

Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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?
Anthony MatovuBusiness Analyst, MTN Uganda

Author

Commented:
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
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.