Although your code appears correct it does not make sense. Usually the best course of action
- Set a breakpoint, when the breakpoint is hit examine objects and values in Visual Studio
local
window. - If that does not help than (and this should be done if if no problems) create a unit test, mockup data and run the LINQ/Lambda to weed out issues.
Since a large majority of developers avoid writing unit test here is a test done in a Windows Form project. The first time we get null as identifier 5 does not exists while the second go around we are not null as id 3 exists.
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void GetPatientButton_Click(object sender, EventArgs e)
{
int identifier = 5;
var pat = Operations.Patients.FirstOrDefault(patient => patient.PatientID == identifier);
Console.WriteLine(pat == null ? $"{identifier} not found" : $"{pat.FirstName} {pat.LastName}");
identifier = 3;
pat = Operations.Patients.FirstOrDefault(patient => patient.PatientID == identifier);
Console.WriteLine(pat == null ? $"{identifier} not found" : $"{pat.FirstName} {pat.LastName}");
}
}
public class Patient
{
public int PatientID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Operations
{
public static List<Patient> Patients => new List<Patient>()
{
new Patient() {PatientID = 1, FirstName = "Joe", LastName = "Jones"},
new Patient() {PatientID = 2, FirstName = "Anne", LastName = "White"},
new Patient() {PatientID = 3, FirstName = "Mary", LastName = "Smith"}
};
}
}
Edit for the sake of argument for .NET Core this is how it's done.
- First two are for those not use to the syntax are checking for null and if not null the variable after
{}
are populated - The last
is null
is clearer
Code
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void GetPatientButton_Click(object sender, EventArgs e)
{
int identifier = 5;
if (Operations.Patients.FirstOrDefault(patient => patient.PatientID == identifier) is {} patientItem1)
{
Debug.WriteLine($"{patientItem1.FirstName} {patientItem1.LastName}");
}
else
{
Debug.WriteLine($"Failed to find {identifier}");
}
identifier = 3;
if (Operations.Patients.FirstOrDefault(patient => patient.PatientID == identifier) is { } patientItem2)
{
Debug.WriteLine($"{patientItem2.FirstName} {patientItem2.LastName}");
}
else
{
Debug.WriteLine($"Failed to find {identifier}");
}
var pat = Operations.Patients.FirstOrDefault(patient => patient.PatientID == identifier);
Debug.WriteLine(pat is null ? $"Failed to find {identifier}" : $"{pat.FirstName} {pat.LastName}");
}
}
public class Patient
{
public int PatientID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Operations
{
public static List<Patient> Patients => new List<Patient>()
{
new Patient() {PatientID = 1, FirstName = "Joe", LastName = "Jones"},
new Patient() {PatientID = 2, FirstName = "Anne", LastName = "White"},
new Patient() {PatientID = 3, FirstName = "Mary", LastName = "Smith"}
};
}
}