Using Lambda expression instead of For Each Loop in ASP.Net

alibasha
 
on Sep 07, 2018 01:52 AM
2330 Views

I have used for each loop and counter, I got code review comment like "please use a lamba expression here, to isolate an occurrence, as opposed to iterating over all and then doing something in response to all found "

Please help how to use lamda expresion here. Thanks.

Private Function DiamondScoreCheckForOdType(ByVal currentPolicyLob As String, ByVal predominantState As String,
                                                               ByVal policyEffectiveDate As Date) As Boolean

            Dim listOfLobForDiamondScoreRule As New HashSet(Of String) From {{getLineOfBusiness(Lob.LineOfBusiness.Auto)},
                                                                                {getLineOfBusiness(AutoNonDealer)},
                                                                                {getLineOfBusiness(Lob.LineOfBusiness.Garage)},
                                                                                {getLineOfBusiness(EmploymentPracticesLiability_EP)},
                                                                                {getLineOfBusiness(GeneralLiability)},
                                                                                {getLineOfBusiness(InlandMarine)},
                                                                                {getLineOfBusiness(PesticideHerbicide)},
                                                                                {getLineOfBusiness([Property])},
                                                                                {getLineOfBusiness(Umbrella)}}
            Dim lobList As New List(Of String)
            Dim eligibleLobCheckCounter As Integer = 0
            lobList = currentPolicyLob.Split(ASTERISK).ToList()

            For Each currentLob In lobList
                listOfLobForDiamondScoreRule.Contains(currentLob)
                If (listOfLobForDiamondScoreRule.Contains(currentLob)) Then
                    eligibleLobCheckCounter = eligibleLobCheckCounter + 1
                End If
            Next

            If (eligibleLobCheckCounter > 0) Then
                Dim isStateAndEffDateEligible As Boolean = (New LobCO.Shared.COShared).IsStateRuleApplicable(getLineOfBusiness(Lob.LineOfBusiness.Common),
                                                                                                                             predominantState,
                                                                                                                             policyEffectiveDate,
                                                                                                                             DiamondScoreRuleCheckForOdType)
                Return isStateAndEffDateEligible
            End If

            Return False

        End Function

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 07, 2018 01:53 AM

Hi alibasha,

I will get back to you soon.

alibasha
 
on Sep 07, 2018 06:08 AM

Hi Dharmendr,

Thanks for your response. I am waiting for your reply. Kindly do the needful. Thanks in advance.

dharmendr
 
on Sep 07, 2018 09:23 AM

I will get back to you tomorrow.

alibasha
 
on Sep 07, 2018 10:00 AM

Thanks for the help. I have achieved this, I will post the code snippet Monday and will mark it as answered. Thanks again.

dharmendr
 
on Sep 09, 2018 04:29 AM

Hi alibasha,

Please check the below example and modify your code.

VB.Net

    Private ASTERISK As Char = ","c
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim currentPolicyLob As String = "GeneralLiability,Property,Umbrella,Test,test1"
        Dim value As Boolean = DiamondScoreCheckForOdType(currentPolicyLob, "", DateTime.Now)
    End Sub

    Private Function DiamondScoreCheckForOdType(ByVal currentPolicyLob As String, ByVal predominantState As String, ByVal policyEffectiveDate As DateTime) As Boolean
        Dim listOfLobForDiamondScoreRule As HashSet(Of String) = New HashSet(Of String)() From {
                                         {"Auto"},{"AutoNonDealer"},{"Garage"},{"GeneralLiability"},{"InlandMarine"},{"PesticideHerbicide"},{"Property"},{"Umbrella"}}
        Dim lobList As List(Of String) = New List(Of String)()
        lobList = currentPolicyLob.Split(ASTERISK).ToList()
        ' Count without loop.
        Dim eligibleLobCheckCounter As Integer = lobList.Where(Function(t2) listOfLobForDiamondScoreRule.Any(Function(t1) t2.Contains(t1))).Count()

        If (eligibleLobCheckCounter > 0) Then
            Dim isStateAndEffDateEligible As Boolean = (New LobCO.Shared.COShared).IsStateRuleApplicable(getLineOfBusiness(Lob.LineOfBusiness.Common), predominantState,policyEffectiveDate,DiamondScoreRuleCheckForOdType)
            Return isStateAndEffDateEligible
        End If

        Return False
    End Function

C#

char ASTERISK = ',';
protected void Page_Load(object sender, EventArgs e)
{

    string currentPolicyLob = "GeneralLiability,Property,Umbrella,Test,test1";
    bool value = DiamondScoreCheckForOdType(currentPolicyLob, "", DateTime.Now);
}

private bool DiamondScoreCheckForOdType(string currentPolicyLob, string predominantState, DateTime policyEffectiveDate)
{
    HashSet<string> listOfLobForDiamondScoreRule = new HashSet<string>() {
                                                    { "Auto" },
                                                    { "AutoNonDealer" },
                                                    { "Garage" },
                                                    { "GeneralLiability" },
                                                    { "InlandMarine" },
                                                    { "PesticideHerbicide" },
                                                    { "Property" },
                                                    { "Umbrella" } };
    List<string> lobList = new List<string>();
    lobList = currentPolicyLob.Split(ASTERISK).ToList();
    // Count without loop.
    int eligibleLobCheckCounter = lobList.Where(t2 => listOfLobForDiamondScoreRule.Any(t1 => t2.Contains(t1))).Count();
    if ((eligibleLobCheckCounter > 0))
    {
        bool isStateAndEffDateEligible = (new LobCO.Shared.COShared()).IsStateRuleApplicable(getLineOfBusiness(Lob.LineOfBusiness.Common), predominantState, policyEffectiveDate, DiamondScoreRuleCheckForOdType);
        return isStateAndEffDateEligible;
    }

    return false;
}