在c#列表中查找,但不同的搜索词 List newList=newList(); 在eduList.FindAll(s=> s、 EventClassID.Equals(cla) &&s.LocationID等于(loc) &&s.EducatorID.Equals(教育程度))) 新增(e);
cla、loc、edu可以是(null或空)或提供值-- 基本上,如果cla、loc、edu都为空,我如何简单地返回原始列表(eduList) 或按loc搜索、按loc搜索、edu搜索、按edu搜索、cla搜索等 我的示例代码仅在所有3个变量都有值时生成一个新列表-- 有没有一种优雅的方法可以做到这一点,而不必使用强力if语句?在c#列表中查找,但不同的搜索词 List newList=newList(); 在eduList.FindAll(s=> s、 EventClassID.Equals(cla) &&s.LocationID等于(loc) &&s.EducatorID.Equals(教育程度))) 新增(e);,c#,list,C#,List,cla、loc、edu可以是(null或空)或提供值-- 基本上,如果cla、loc、edu都为空,我如何简单地返回原始列表(eduList) 或按loc搜索、按loc搜索、edu搜索、按edu搜索、cla搜索等 我的示例代码仅在所有3个变量都有值时生成一个新列表-- 有没有一种优雅的方法可以做到这一点,而不必使用强力if语句?List newList=eduList.FindAll(s=> List<DTOeduevent> newList = new List<DT
List newList=eduList.FindAll(s=>
List<DTOeduevent> newList = new List<DTOeduevent>();
foreach (DTOeduevent e in eduList.FindAll(s =>
s.EventClassID.Equals(cla)
&& s.LocationID.Equals(loc)
&& s.EducatorID.Equals(edu)))
newList.Add(e);
(cla==null | | s.EventClassID.Equals(cla))
&&(loc==null | | s.LocationID.Equals(loc))
&&(edu==null | | s.eductorID.Equals(edu));
假设值是可为null的值类型或类。如果它们是字符串,您可以将cla==null
替换为String.IsNullOrEmpty(cla)
List newList=eduList.FindAll(s=>
(cla==null | | s.EventClassID.Equals(cla))
&&(loc==null | | s.LocationID.Equals(loc))
&&(edu==null | | s.eductorID.Equals(edu));
假设值是可为null的值类型或类。如果它们是字符串,您可以将cla==null
替换为String.IsNullOrEmpty(cla)
IEnumerable newList=eduList;
如果(cla!=null)
{
newList=newList.Where(s=>s.EventClassID==cla);
}
如果(loc!=null)
{
newList=newList.Where(s=>s.LocationID==loc);
}
如果(edu!=null)
{
newList=newList.Where(s=>s.eductorID==edu);
}
newList=newList.ToList();
由于延迟执行,当您调用ToList
时,Where
语句应该同时执行;它将只在原始列表中执行一个循环。IEnumerable newList=eduList;
如果(cla!=null)
{
newList=newList.Where(s=>s.EventClassID==cla);
}
如果(loc!=null)
{
newList=newList.Where(s=>s.LocationID==loc);
}
如果(edu!=null)
{
newList=newList.Where(s=>s.eductorID==edu);
}
newList=newList.ToList();
由于延迟执行,当您调用
ToList
时,Where
语句应该同时执行;它只会在原始列表中执行一个循环。我个人倾向于封装您在这里似乎正在执行的操作的逻辑:检查找到的id是否等于某个搜索id。唯一的问题是如何首先在其中获取null或空的检查
一种方法是使用静态扩展方法:
IEnumerable<DTOeduevent> newList = eduList;
if (cla != null)
{
newList = newList.Where(s => s.EventClassID == cla);
}
if (loc != null)
{
newList = newList.Where(s => s.LocationID == loc);
}
if (edu != null)
{
newList = newList.Where(s => s.EducatorID == edu);
}
newList = newList.ToList();
我也倾向于像Domenic一样使用LINQ和IEnumerable,即使您可以让它与List.FindAll一样轻松地工作。下面是一个示例用法:
public static class DtoFilterExtensions
{
public static bool IsIdEqual(this string searchId, string foundId) {
Debug.Assert(!string.IsNullOrEmpty(foundId));
return !string.IsNullOrEmpty(searchId) && foundId.Equals(searchId);
}
}
公共无效过滤器(字符串cla、字符串loc、字符串edu){
var startList=新列表();
var filteredList=startList
其中(x=>x.classId.IsIdEqual(cla)和&x.locationId.IsIdEqual(loc)和&x.educatorId.IsIdEqual(edu));
Show(filteredList.ToList());
}
当然,在您自己的代码中,您已经在成员变量或参数中获得了开始列表,这假设您已经获得了一些方法,比如Show(),您希望在其中对过滤结果进行处理。然后触发延迟执行,正如Domenic在ToList调用中解释的那样(这当然是作为LINQ的一部分提供的另一个扩展方法)
HTH,Berryl我个人倾向于某种封装了您在这里所做的逻辑的东西:检查找到的id是否等于某个搜索id。唯一的问题是如何首先在其中获取null或空的检查 一种方法是使用静态扩展方法:
IEnumerable<DTOeduevent> newList = eduList;
if (cla != null)
{
newList = newList.Where(s => s.EventClassID == cla);
}
if (loc != null)
{
newList = newList.Where(s => s.LocationID == loc);
}
if (edu != null)
{
newList = newList.Where(s => s.EducatorID == edu);
}
newList = newList.ToList();
我也倾向于像Domenic一样使用LINQ和IEnumerable,即使您可以让它与List.FindAll一样轻松地工作。下面是一个示例用法:
public static class DtoFilterExtensions
{
public static bool IsIdEqual(this string searchId, string foundId) {
Debug.Assert(!string.IsNullOrEmpty(foundId));
return !string.IsNullOrEmpty(searchId) && foundId.Equals(searchId);
}
}
公共无效过滤器(字符串cla、字符串loc、字符串edu){
var startList=新列表();
var filteredList=startList
其中(x=>x.classId.IsIdEqual(cla)和&x.locationId.IsIdEqual(loc)和&x.educatorId.IsIdEqual(edu));
Show(filteredList.ToList());
}
当然,在您自己的代码中,您已经在成员变量或参数中获得了开始列表,这假设您已经获得了一些方法,比如Show(),您希望在其中对过滤结果进行处理。然后触发延迟执行,正如Domenic在ToList调用中解释的那样(这当然是作为LINQ的一部分提供的另一个扩展方法)
HTH,贝里尔