Reputation:
I want to search for all records which are null, or not null. I can do this that way (sample code):
public ICollection<Employees> SearchForPhoneNumberNull()
{
return collection.Where(x => x.PhoneNumber == null);
}
public ICollection<Employees> SearchForPhoneNumberNotNull()
{
return collection.Where(x => x.PhoneNumber != null);
}
Can i somehow connect these two methods, and for example send some variable that will decide is it !=
or ==
?
Upvotes: 0
Views: 112
Reputation: 156
For single function to handle both null and non-null Phone Numbers, I would implement an extension function.
static class EmployeesExtensions
{
public static ICollection<Employees> SearchForPhoneNumber(
this ICollection<Employees> collection,
Func<Employees, bool> predicate)
{
return collection.Where(predicate).ToList();
}
}
There are a few important things to note here.
Extension functions must be in a static class (i.e., static class EmployeesExtensions
this ICollection<Employees> collection
this
enables the function to be used as an extension function.Func<Employees, bool> predicate
bool
(i.e., True/False). e => e.PhoneNumber == null
.Where()
by name, predicate
.ToList()
is needed to convert the IEnumerable<Employees>
to ICollection<Employees>
. However, this forces the evaluation of the LINQ expression tree that you are building. So, rather than returning a composable expression tree, it returns the actual Employees
objects.So, now that we have an extension method, we can use it in a program.
class Program
{
static void Main(string[] args)
{
var employees = CreateEmployeesCollection();
Console.WriteLine("Employees with Phone Number");
foreach (var e in employees.SearchForPhoneNumber(
e => !string.IsNullOrEmpty(e.PhoneNumber)))
{
Console.WriteLine($"ID: {e.Id}; Name: {e.Name}; Phone: {e.PhoneNumber}");
}
Console.WriteLine();
Console.WriteLine("Employees with No Phone Number");
foreach (var e in employees.SearchForPhoneNumber(
e => string.IsNullOrEmpty(e.PhoneNumber)))
{
Console.WriteLine($"ID: {e.Id}; Name: {e.Name}; Phone: null");
}
Console.WriteLine();
Console.WriteLine("Employees with Phone Number in 312 Area Code");
foreach (var e in employees.SearchForPhoneNumber(
e => !string.IsNullOrEmpty(e.PhoneNumber)
&& e.PhoneNumber.StartsWith("312")))
{
Console.WriteLine($"ID: {e.Id}; Name: {e.Name}; Phone: {e.PhoneNumber}");
}
}
private static ICollection<Employees> CreateEmployeesCollection()
{
var employees = new Collection<Employees>();
employees.Add(new Employees { Id = 1, Name = "John Doe", PhoneNumber = null });
employees.Add(new Employees { Id = 2, Name = "Jane Doe", PhoneNumber = "212-555-1212" });
employees.Add(new Employees { Id = 3, Name = "Mike Smith", PhoneNumber = "312-555-1213" });
employees.Add(new Employees { Id = 4, Name = "Mary Smith", PhoneNumber = "312-555-1214" });
employees.Add(new Employees { Id = 5, Name = "Bob Jones", PhoneNumber = "214-555-1215" });
employees.Add(new Employees { Id = 5, Name = "Beth Jones", PhoneNumber = null });
return employees;
}
}
Oh, also, for this example, I assumed the following definition of the Employees class
// File: Employees.cs
class Employees
{
public int Id { get; set; }
public string Name { get; set; }
public string PhoneNumber { get; set; }
}
Upvotes: 0
Reputation: 3473
You can use bool
argument to verify if the phone number should be null
or not.
Also the Where
method returns IEnumerable
, there is no explicit converstion to ICollection
, thus I changed the method's declaration
public IEnumerable<Employees> SearchForPhoneNumber(bool isNull = true)
{
return collection.Where(x => isNull ? x.PhoneNumber == null : x.PhoneNumber != null);
}
Upvotes: 0
Reputation: 3019
Sure, like this:
public ICollection<Employees> SearchForPhoneNumber(bool nullOnly)
{
return collection.Where(x => nullOnly ? x.PhoneNumber == null : x.PhoneNumber != null);
}
Upvotes: 2