locked
How to write function in c# using If condition RRS feed

  • Question

  • User105364981 posted

    i have the data if AFG is Yes or Type is N/A then we need to count as 1 valid record
    Condition 1
    AFG Type
    yes N/A

    COnditon 2 - If AGF is No or Type is Phys then we need to count as another valid record
    AFG Type
    No Phys

    Also for these both conditions we have to check the record is active or not, for that we need to check start date should be less than current date and end date should be future data.. if it is valid then only we can check both the oncditons                     

                 
    Tuesday, October 30, 2018 10:00 PM

All replies

  • User-1716253493 posted

    SELECT COUNT(CASE WHEN getdate>datecol and active=1 and [type]='afg' and phyis='n/a' THEN 1 WHEN getdate>datecol and active=1 and [type]='afg' and phyis='no' THEN anothercolom ELSE 0 END)

    Tuesday, October 30, 2018 10:44 PM
  • User303363814 posted

    Show the code you have so far, we can help you complete it.  Without any knowledge of the variables or types that you already have it is just guessing … but here goes

    if ((startDate < DateTime.Now && endDate > DateTime.Now) &&  
        ((AFG == "Yes" && Type == "N/A") ||
         (AFG == "No" && Type == "No Phys"))) {
    // Valid 
    } else {
    // Not valid
    }

    It is not clear if you are checking AFG to be "Yes" or "yes".  If only one is correct then you need to get the case right.  If you don't care about the case then you could use .ToLowerCase() to normalise the data.  IF you only care about the date (and not the date+time) then use DateTime.Today.  

    What if StartDate is the current day?  Is that an active record?  Your specification says "less than", so it is not.

    Tuesday, October 30, 2018 11:15 PM
  • User839733648 posted

    Hi mcfarland___,

    According to your description, I suggest that you could use simple if-statement to meet your requirement.

                int count1 = 0;//count as 1 valid record
                int count2 = 0;//count as another valid record
                if (startdate<DateTime.Now&&enddate> DateTime.Now)
                {
                    if(data.AFG == "Yes"|| data.Type == "N/A")
                    {
                        //valid record
                        count1++;
                        
                    }
                    else if(data.AFG == "No" || data.Type == "Phys")
                    {
                        //valid record
                        count2++;                    
                    }
                    else
                    {
                        //invalid record
                    }
                }
                else
                {
                    //invalid record
                }

    Best Regards,

    Jenifer

    Wednesday, October 31, 2018 7:05 AM