C# 枚举中的参数。其中

C# 枚举中的参数。其中,c#,.net,linq,C#,.net,Linq,我想搜索所有为空或不为空的记录。我可以这样做(示例代码): public ICollection SearchForPhoneNumberNull() { 返回集合。其中(x=>x.PhoneNumber==null); } public ICollection SearchForPhoneNumberNotNull() { 返回集合。其中(x=>x.PhoneNumber!=null); } 我是否可以将这两种方法连接起来,例如发送一些变量来决定它是=或== 当然,像这样: public

我想搜索所有为空或不为空的记录。我可以这样做(示例代码):

public ICollection SearchForPhoneNumberNull()
{
返回集合。其中(x=>x.PhoneNumber==null);
}
public ICollection SearchForPhoneNumberNotNull()
{
返回集合。其中(x=>x.PhoneNumber!=null);
}
我是否可以将这两种方法连接起来,例如发送一些变量来决定它是
=
==

当然,像这样:

public ICollection<Employees> SearchForPhoneNumber(bool nullOnly)
{
    return collection.Where(x => nullOnly ? x.PhoneNumber == null : x.PhoneNumber != null);
}
public ICollection SearchForPhoneNumber(仅限布尔空值)
{
返回集合。其中(x=>nullOnly?x.PhoneNumber==null:x.PhoneNumber!=null);
}
当然,像这样:

public ICollection<Employees> SearchForPhoneNumber(bool nullOnly)
{
    return collection.Where(x => nullOnly ? x.PhoneNumber == null : x.PhoneNumber != null);
}
public ICollection SearchForPhoneNumber(仅限布尔空值)
{
返回集合。其中(x=>nullOnly?x.PhoneNumber==null:x.PhoneNumber!=null);
}

您可以使用
bool
参数验证电话号码是否应为
null

还有
Where
方法返回
IEnumerable
,没有与
ICollection
的显式对话,因此我更改了方法的声明

        public IEnumerable<Employees> SearchForPhoneNumber(bool isNull = true)
        {
            return collection.Where(x => isNull ? x.PhoneNumber == null : x.PhoneNumber != null);
        }
public IEnumerable SearchForPhoneNumber(bool isNull=true)
{
返回集合。其中(x=>isNull?x.PhoneNumber==null:x.PhoneNumber!=null);
}

您可以使用
bool
参数验证电话号码是否应为
null

还有
Where
方法返回
IEnumerable
,没有与
ICollection
的显式对话,因此我更改了方法的声明

        public IEnumerable<Employees> SearchForPhoneNumber(bool isNull = true)
        {
            return collection.Where(x => isNull ? x.PhoneNumber == null : x.PhoneNumber != null);
        }
public IEnumerable SearchForPhoneNumber(bool isNull=true)
{
返回集合。其中(x=>isNull?x.PhoneNumber==null:x.PhoneNumber!=null);
}

对于处理空和非空电话号码的单一功能,我将实现一个扩展功能

静态类EmployeesExtrains
{
公共静态ICollection SearchForPhoneNumber(
此ICollection集合,
Func谓词)
{
返回集合.Where(谓词).ToList();
}
}
这里有一些重要的事情需要注意

  • 扩展函数必须在静态类中(即,
    静态类EmployeesExtensions

  • 此ICollection集合

    • 定义可调用扩展函数的对象类型
    • 允许将该函数用作扩展函数
  • Func谓词
    • 允许将lambda表达式传递给扩展函数。
      • 将Employees对象作为参数
      • 返回一个布尔值(即真/假)
    • 示例:
      e=>e.PhoneNumber==null
    • 通过名称传递到
      。其中()
      谓词
  • .ToList()
    需要将
    IEnumerable
    转换为
    ICollection
    。但是,这会强制计算您正在构建的LINQ表达式树。因此,它不会返回可组合表达式树,而是返回实际的
    Employees
    对象
现在我们有了一个扩展方法,我们可以在程序中使用它

类程序
{
静态void Main(字符串[]参数)
{
var employees=CreateEmployeesCollection();
Console.WriteLine(“有电话号码的员工”);
foreach(在employees.SearchForPhoneNumber中的变量e(
e=>!string.IsNullOrEmpty(e.PhoneNumber)))
{
WriteLine($“ID:{e.ID};Name:{e.Name};Phone:{e.PhoneNumber}”);
}
Console.WriteLine();
Console.WriteLine(“没有电话号码的员工”);
foreach(在employees.SearchForPhoneNumber中的变量e(
e=>string.IsNullOrEmpty(e.PhoneNumber)))
{
WriteLine($“ID:{e.ID};Name:{e.Name};Phone:null”);
}
Console.WriteLine();
Console.WriteLine(“电话号码为312区号的员工”);
foreach(在employees.SearchForPhoneNumber中的变量e(
e=>!string.IsNullOrEmpty(e.PhoneNumber)
&&e.PhoneNumber.StartsWith(“312”))
{
WriteLine($“ID:{e.ID};Name:{e.Name};Phone:{e.PhoneNumber}”);
}
}
私有静态ICollection CreateEmployeesCollection()
{
var employees=新集合();
添加(新员工{Id=1,Name=“John Doe”,PhoneNumber=null});
添加(新员工{Id=2,Name=“Jane Doe”,PhoneNumber=“212-555-1212”});
添加(新员工{Id=3,Name=“Mike Smith”,PhoneNumber=“312-555-1213”});
添加(新员工{Id=4,Name=“Mary Smith”,PhoneNumber=“312-555-1214”});
添加(新员工{Id=5,Name=“Bob Jones”,PhoneNumber=“214-555-1215”});
添加(新员工{Id=5,Name=“Beth Jones”,PhoneNumber=null});
返回员工;
}
}
哦,还有,在这个例子中,我假设Employees类的定义如下

//文件:Employees.cs
班级员工
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串PhoneNumber{get;set;}
}

对于处理空和非空电话号码的单一功能,我将实现一个扩展功能

静态类EmployeesExtrains
{
公共静态ICollection SearchForPhoneNumber(
此ICollection集合,
Func谓词)
{
返回集合.Where(谓词).ToList();
}
}
这里有一些重要的事情需要注意

  • 扩展函数必须位于静态类中(即,
    静态类EmployeesExtensions