How to pass method and if condition inside foreach [Resolved]

Posted by Oswaldlily under ASP.NET on 4/22/2016 | Points: 10 | Views : 1591 | Status : [Member] | Replies : 2
Im doing validation inside foreach loop inside LINQ.
And have called method inside foreach loop which will return string value.
Based on return result it will throw exception.
Folowing code giving me error in the line... if(item ==null) throw exception

private void RunTest()
{
EmployeeCollection empcoll = new EmployeeCollection();
empcoll.Add(new Employee() { EmployeeNumber = "1111", HireDate = DateTime.Now });
empcoll.Add(new Employee() { EmployeeNumber = "3333", HireDate = DateTime.Now });
empcoll.Add(new Employee() { EmployeeNumber = "2222", HireDate = null });
empcoll.Add(new Employee() { EmployeeNumber = "4444", HireDate = null });

//Here's the "money" line!
empcoll.Where(x => x.HireDate.HasValue == false).ToList().ForEach(item => ReportEmployeeWithMissingHireDate(item.EmployeeNumber));
if(item ==null) throw exception
}
public string ReportEmployeeWithMissingHireDate(int EmployeeNumber)
{
if()
{
// some conditions
}
return null
}




Responses

Posted by: Sheonarayan on: 4/23/2016 [Administrator] HonoraryPlatinum | Points: 50

Up
0
Down

Resolved
I would suggest, make your LINQ statement simple first.

Your code is looking mismatch statement, it is difficult to understand as you have not put them under the code block so it is not readable properly.

The approach to solve the problem should be following.

1. Have the ToList collection first.
2. Have a foreach loop and then check for your item. In any case item will not be null in foreach as it loops through each item, if item is null that iteration will not happen.
3. item.HireDate can be null

foreach(var item in Employees)
{
if (item.HireDate == null)
{
// do something
}
}


The whole idea is make each statement simple, do not overcomplicate things by adding so many methods, properties into a single statement. In this way it is difficult to understand the code and where error occurs.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 4/25/2016 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

when you post issue if you post correct post then only reply users find your issue and fixed better.

I change the code check and let me know.



protected void bt1_Click(object sender, EventArgs e)
{
RunTest();
}

private void RunTest()
{
List empcoll = new List();
empcoll.Add(new Employee() { EmployeeNumber = "1111", HireDate = DateTime.Now });
empcoll.Add(new Employee() { EmployeeNumber = "3333", HireDate = DateTime.Now });
empcoll.Add(new Employee() { EmployeeNumber = "2222", HireDate = null });
empcoll.Add(new Employee() { EmployeeNumber = "4444", HireDate = null });
//Here's the "money" line!
empcoll.Where(x => x.HireDate.HasValue == false).ToList().ForEach(item => ReportEmployeeWithMissingHireDate(item.EmployeeNumber));
//if(item ==null) throw exception
}

public string ReportEmployeeWithMissingHireDate(string EmployeeNumber)
{
if (EmployeeNumber==null)
{
// pass your error string here...
}
return null;
}

Mark as Answer if its helpful to you

Kumaraspcode2009@gmail.com

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response