如何在onienumerable上应用动态过滤器? [英] How to apply a dynamic filter on ienumerable?
问题描述
大家好,
如何在IEnumerable上应用过滤器? GetTaskAllocatedOnFilter属性列表任何值都可以为null我需要一个数据
请帮忙,也知道你们是否想要任何其他信息来证明我的解决方案在这个
我尝试过:
公共类GetTaskAllocatedOnFilter
{
公共指导? Project_ID {get;组; }
公共指导? Emp_ID {get;组; }
公共指导? Task_ID {get;组; }
}
public IEnumerable< task_allocationmodel> GetTaskAllocatedList(GetTaskAllocatedOnFilter getOnFilter)
{
_DALTaskAllocation = new DALTaskAllocation();
DataTable taskAllocatedList = _DALTaskAllocation.GetTaskAllocatedList(getOnFilter);
IEnumerable< task_allocationmodel> IEtaskAllocationList = taskAllocatedList.AsEnumerable()。选择(row => new Task_AllocationModel(){
Emp_ID = new Guid(row [Emp_ID]。ToString()),
Emp_Name = row [Emp_Name]。ToString(),
Hrs = Convert.ToInt16(row [Hrs]),
Location = row [位置]。ToString(),
Location_ID = Convert.ToInt16(row [Location_ID]),
Month = Convert.ToInt16(row [Month] .ToString()),
Project_ID = new Guid(row [Project_ID]。ToString()),
Project_Name = row [Project_Name]。ToString( ),
Reporting_Manager = row [Reporting_Manager]。ToString(),
Task_ID = new Guid(row [Task_ID]。ToString()),
Total = Convert.ToInt16(row [Total]。ToString()),
Type_ID = Convert.ToInt16(row [Type_ID]。ToString()),
年= Convert.ToInt16(row [Year]。ToString()),
Createddate = Convert.ToDateTime(row [Createddate]。ToString()),>
CreatedBy = row [CreatedBy]。ToString()
});
if((Guid.Empty == getOnFilter.Emp_ID&& ; Guid.Empty == getOnFilter.Project_ID)&& Guid.Empty!= getOnFilter.Task_ID)
if((Guid.Empty == getOnFilter.Emp_ID&&& Guid.Empty == getOnFilter.Task_ID)& ;& Guid.Empty!= getOnFilter.Project_ID)
if((Guid.Empty == getOnFilter.Project_ID&& Guid.Empty == getOnFilter。 Task_ID)&& Guid.Empty!= getOnFilter.Emp_ID)
if((Guid.Empty == getOnFilter.Project_ID&& Guid.Empty = = getOnFilter.Task_ID)&& Guid.Empty!= getOnFilter.Emp_ID)
返回IEtaskAllocationList;
}
Hi Guys,
How can i apply a filter on "IEnumerable"? "GetTaskAllocatedOnFilter" property list any value can be null basis on that i need a data
Please help, also let know if you guys want any other information to proved me solution on this
What I have tried:
public class GetTaskAllocatedOnFilter
{
public Guid? Project_ID { get; set; }
public Guid? Emp_ID { get; set; }
public Guid? Task_ID { get; set; }
}
public IEnumerable<task_allocationmodel> GetTaskAllocatedList(GetTaskAllocatedOnFilter getOnFilter)
{
_DALTaskAllocation = new DALTaskAllocation();
DataTable taskAllocatedList = _DALTaskAllocation.GetTaskAllocatedList(getOnFilter);
IEnumerable<task_allocationmodel> IEtaskAllocationList = taskAllocatedList.AsEnumerable().Select(row => new Task_AllocationModel() {
Emp_ID = new Guid(row["Emp_ID"].ToString()),
Emp_Name = row["Emp_Name"].ToString(),
Hrs = Convert.ToInt16(row["Hrs"]),
Location = row["Location"].ToString(),
Location_ID = Convert.ToInt16(row["Location_ID"]),
Month = Convert.ToInt16(row["Month"].ToString()),
Project_ID = new Guid(row["Project_ID"].ToString()),
Project_Name = row["Project_Name"].ToString(),
Reporting_Manager = row["Reporting_Manager"].ToString(),
Task_ID = new Guid(row["Task_ID"].ToString()),
Total = Convert.ToInt16(row["Total"].ToString()),
Type_ID = Convert.ToInt16(row["Type_ID"].ToString()),
Year = Convert.ToInt16(row["Year"].ToString()),
Createddate = Convert.ToDateTime(row["Createddate"].ToString()),
CreatedBy = row["CreatedBy"].ToString()
});
if ((Guid.Empty == getOnFilter.Emp_ID && Guid.Empty == getOnFilter.Project_ID) && Guid.Empty != getOnFilter.Task_ID)
if ((Guid.Empty == getOnFilter.Emp_ID && Guid.Empty == getOnFilter.Task_ID) && Guid.Empty != getOnFilter.Project_ID)
if ((Guid.Empty == getOnFilter.Project_ID && Guid.Empty == getOnFilter.Task_ID) && Guid.Empty != getOnFilter.Emp_ID)
if ((Guid.Empty == getOnFilter.Project_ID && Guid.Empty == getOnFilter.Task_ID) && Guid.Empty != getOnFilter.Emp_ID)
return IEtaskAllocationList;
}
推荐答案
您可以使用.Where
扩展方法: Enumerable.Where(TSource)方法(IEnumerable(TSource),Func(TSource,Boolean))(System.Linq) [ ^ ]:
You can use the.Where
extension method: Enumerable.Where(TSource) Method (IEnumerable(TSource), Func(TSource, Boolean)) (System.Linq)[^]:
using System.Linq;
// Example:
IEnumerable<int> old = new int[] { 0, 1, 2, 3 };
IEnumerable<int> _new = old.Where(x => x != 0);
在上面的示例中, _new
将包含 old
中的所有元素,但 0
除外: x $ c $的谓词c>是
x!= 0
。您可以将此过滤示例调整为您的代码库。
In the above example, _new
will contain all elements from old
except 0
: the predicate for x
is x != 0
. You can adapt this filtering example to your codebase.
这篇关于如何在onienumerable上应用动态过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!