質問

I have this method which through it, I guess it fills reportviewer parameters, but when I evaluate ReportParameters, (this.reportViewer1.LocalReport.GetParameters()), the first parameter has value of all others parameters. What's wrong here? this code belongs to FrmReport(example):

public void SetReportParameters(params string[] parameters){
    if (parameters.Length == ReportParameters.Count)
    {
        //ReportParameters = this.reportViewer1.LocalReport.GetParameters();
        ReportParameterCollection pCol = new ReportParameterCollection();
        int i = 0;
        parameters.ToList().ForEach(p => {
            pCol.Add(new ReportParameter(ReportParameters[i].Name, p));
        });
        this.reportViewer1.LocalReport.SetParameters(pCol);
    }
}

Then I say on another form:

var frmReport = new FrmReport();
frmReport.SetReportParameters("1","2");

And there I get the result like I said before.

役に立ちましたか?

解決

I think this would fall under a "misuse of ForEach" category. You are never incrementing i first of all (that I can see) which is your main problem. The major disadvantage to .ToList().ForEach() is it's poor readability.

I think you would be better off just using loops first until you are getting the results you want, then if you really must go ahead and convert to LINQ.

    var pCol = new ReportParameterCollection();
    for (int i = 0; i < parameters.Length; i++)
    {
        pCol.Add(new ReportParameter(ReportParameters[i].Name, parameters[i]);
    }

Really not sure why you are checking the length of ReportParameters and parameters. The code is fairly unclear and your intent is not written out that I can see. But I would "un-optimize" this code so that it is easier to step through in the debugger then go for optimization later when your method is working properly.

他のヒント

Well, problem with ForEach is solved.

int i = 0;
parameters.ToList().ForEach(p => {
    pCol.Add(new ReportParameter(ReportParameters[i].Name, p));
    i++;
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top