none
How to simplify existing method using Lambda Expression RRS feed

  • Question

  • I have a method which return a bool value found from patientlist's item found by patientid.

    Here is the code.

    public bool GetAvoidReminder(long patientid, List<Patient> patientlist)
    {
        foreach (Patient patient in patientlist)
        {
            if (patient.PatientID == patientid)
            {
                return (patient.AvoidReminder);
            }
        }
        return (false);
    }
    

    Calling part is like following.

    bool bret = GetAvoidReminder(patientid, patientlist);

    I'd like to simplify these code using Lambda Expression or other way.

    Can anybody give me any advice?

    Saturday, July 11, 2020 8:17 AM

Answers

  • Use the following.

    public bool GetAvoidReminder(long patientid, List<Patient> patientlist)
    {
        var patient = patientlist.FirstOrDefault(p => p.PatientID == patientid);
        if (patient != null)
        {
            return patient.AvoidReminder;
        }
        else
        {
            return (false);
        }
    
        
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Saturday, July 11, 2020 10:00 AM
    Moderator
  • Alternative

    public bool GetAvoidReminder(long patientid, List<Patient> patientlist) =>
    	 patientlist.Where(p => p.PatientID == patientid)
    	 			.Select(p=>p.AvoidReminder)
    				.DefaultIfEmpty(false)
    				.Single();

    Or

    public bool GetAvoidReminder(long patientid, List<Patient> patientlist) =>
    	 patientlist.SingleOrDefault(p => p.PatientID == patientid)?.AvoidReminder??false;


    www.cihanyakar.com


    • Edited by Cihan YakarMVP Saturday, July 11, 2020 2:36 PM
    • Marked as answer by Jeff0803 Sunday, July 12, 2020 9:37 AM
    Saturday, July 11, 2020 2:34 PM

All replies

  • Use the following.

    public bool GetAvoidReminder(long patientid, List<Patient> patientlist)
    {
        var patient = patientlist.FirstOrDefault(p => p.PatientID == patientid);
        if (patient != null)
        {
            return patient.AvoidReminder;
        }
        else
        {
            return (false);
        }
    
        
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Saturday, July 11, 2020 10:00 AM
    Moderator
  • Alternative

    public bool GetAvoidReminder(long patientid, List<Patient> patientlist) =>
    	 patientlist.Where(p => p.PatientID == patientid)
    	 			.Select(p=>p.AvoidReminder)
    				.DefaultIfEmpty(false)
    				.Single();

    Or

    public bool GetAvoidReminder(long patientid, List<Patient> patientlist) =>
    	 patientlist.SingleOrDefault(p => p.PatientID == patientid)?.AvoidReminder??false;


    www.cihanyakar.com


    • Edited by Cihan YakarMVP Saturday, July 11, 2020 2:36 PM
    • Marked as answer by Jeff0803 Sunday, July 12, 2020 9:37 AM
    Saturday, July 11, 2020 2:34 PM