I have this code where I am iterating over objects using foreach
loop
public ActionResult userDocs()
{
var user = Server.HtmlEncode(Request.Cookies["UserName"].Value);
var role = db.userdetails.FirstOrDefault(s => s.UserName == user).FullName;
var test = "kokulchellamuthu1programmer";
var find = db.ApplySchedule.Where(x => x.CC.Contains(test)).ToList();
foreach(var i in find)
{
var res = db.docinfo.Where(z => z.RequestID == i.BookingID).ToList();
}
return View(res);
}
Now I want to return the res
variable to view
in order to display matching downloadable documents to the user, I am unable to return it to the view as the code doesn’t compile, can someone please help me with this?
Thanks.
2
Answers
You’ve defined the
res
variable inside theforeach
block so it’s inaccessible in yourreturn res;
statement. Lift theres
out of there, so it’d look like this:However, I think you don’t even really need the
foreach
. You may be able to just do:If that results in an invalid operation exception or similar then the first part of the answer should still resolve the issue.
You must define an empty list before Foreach and add that item to the defined list for each item found equal to your condition. According to the code below :