如何使用一个带有空值的参数列表的C#方法? [英] How can I use one C# method with list of parametes with null value?
本文介绍了如何使用一个带有空值的参数列表的C#方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有4个输入值employeeid,requestnumber,startdate,enddate
i创建了一个方法,就像它返回结果基于参数i传递给它就好像我需要员工ID的结果所以我只需要通过
BindingGrid(null,null,null,employeeid)
或
BindingGrid(null,null,requestnumber,null)
或
BindingGrid(startdate,enddate,null,null)
我尝试过:
< pre lang =c#>
< pre> public void BindingGrid(DateTime?start,DateTime?end,int?requestnumber,string?employeeid)
{
var query = ctx.tbl.Where(x =>
(
x.CreatedDate.Value.Year> = start.Value.Year&& x.CreatedDate.Value.Month> = start.Value.Month&& x.CreatedDate.Value.Day> ; = start.Value.Day
)
&&
(
x.CreatedDate.Value.Year< = end.Value.Year&&
x.CreatedDate.Value.Month< = end.Value.Month&&
x.CreatedDate.Value.Day< = end.Value.Day
)
& ;&(x.resuestid = requestnumber)
//此抛出错误:string.contains(string)的最佳重载方法匹配具有无效参数
&&(x.RequestorEmployeeID .Contains(employeeid)))。ToList();
gridReport.DataSource = query;
gridReport.DataBind();
}
解决方案
您正在使用可空类型而不检查它是否具有值:
var query = ctx.tbl.Where(x = > x.CreatedDate.HasValue&&
(x.CreatedDate.Value.Year > = start.Value.Year &&
x.CreatedDate.Value.Month> = start.Value.Month&&
x.CreatedDate.Value.Day > =start.Value.Day)&&
(x.CreatedDate.Value.Year < = end.Value.Year& &
x.CreatedDate.Value.Month < = end.Value.Month&&
x.CreatedDate.Value.Day < = end.Value.Day)&&
(x.resuestid == requestnumber)&&
(x。 RequestorEmployeeID.Contains(雇员)));集团kquote>
i have 4 inputs values employeeid,requestnumber,startdate,enddate
i created a method to act like it return result based on parameter i pass to it like if i need result by employee id so i just pass
BindingGrid(null,null,null, employeeid)
or
BindingGrid(null,null,requestnumber,null)
or
BindingGrid(startdate,enddate,null,null)
What I have tried:
<pre lang="c#"> <pre> public void BindingGrid(DateTime? start,DateTime? end,int? requestnumber,string? employeeid) { var query = ctx.tbl.Where(x => ( x.CreatedDate.Value.Year >= start.Value.Year && x.CreatedDate.Value.Month>=start.Value.Month && x.CreatedDate.Value.Day >=start.Value.Day ) && ( x.CreatedDate.Value.Year <= end.Value.Year && x.CreatedDate.Value.Month <= end.Value.Month && x.CreatedDate.Value.Day <= end.Value.Day ) && ( x.resuestid=requestnumber) //this throw error : best overload method match for string.contains(string) has invalid arguments && ( x.RequestorEmployeeID.Contains(employeeid) ) ).ToList(); gridReport.DataSource = query; gridReport.DataBind(); }解决方案You are using nullable types and not checking if it has a value:
var query = ctx.tbl.Where(x => x.CreatedDate.HasValue && (x.CreatedDate.Value.Year >= start.Value.Year && x.CreatedDate.Value.Month>=start.Value.Month && x.CreatedDate.Value.Day >=start.Value.Day) && (x.CreatedDate.Value.Year <= end.Value.Year && x.CreatedDate.Value.Month <= end.Value.Month && x.CreatedDate.Value.Day <= end.Value.Day) && (x.resuestid == requestnumber) && (x.RequestorEmployeeID.Contains(employeeid)));
这篇关于如何使用一个带有空值的参数列表的C#方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文