You must Sign In to post a response.
  • Category: ASP.NET

    How to pass method and if condition inside foreach

    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
    }
  • #765626
    What error application gives you ? Please share error text so that we can help you better in order to resolve the issue
    in your code you have used 'throw exception' but have you used Try..Catch in your code, Throw new Exception will work only with try catch.
    Please check and revert.

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765629
    Hi
    Which you post the code already erorr

    Error1: your code
    We cant do this-> class object canot do Add property check them . i mention code below

    EmployeeCollection empcoll = new EmployeeCollection();
    empcoll.Add(


    Error2: your code

    You receive employee number is integer but in your code above assign string format this is another error

    int EmployeeNumber


    try this code


    public string ReportEmployeeWithMissingHireDate(string EmployeeNumber)
    {
    if (EmployeeNumber == null)
    {
    LBLERROR.Text = "pass your Error";
    }
    else
    {
    //Process somthing
    }
    return null;
    }

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #765632
    @Dot net developer :

    Thanks for you reply.. That is just sample code. Major thing I request to focus is: How to call return function inside this LINQ query..
    empcoll.Where(x => x.HireDate.HasValue == false).ToList().ForEach(item => ReportEmployeeWithMissingHireDate(item.EmployeeNumber));

    I'm not sure whether "item" has the return value from this function. (item => ReportEmployeeWithMissingHireDate(item.EmployeeNumber))

    Based on the return function, I need to write if condition inside the same foreach loop as below :
    if(item ==null) throw exception

    This is the area I strucked now.,

  • #765634
    If you want to get the return value from funtion 'ReportEmployeeWithMissingHireDate' then instead of ForEach you can try 'select' as below

    empcoll.Where(x => x.HireDate.HasValue == false).ToList().Select(item => ReportEmployeeWithMissingHireDate(item.EmployeeNumber));

    Now, coming to your error, if condition you have mentioned could be false in your function. You can return an EmployeeNumber instead of null just for debug purpose let let me know the detail exception .

  • #765641
    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<Employee> empcoll = new List<Employee>();
    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;
    }

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #765649
    First you have to find the correct error message.
    Either you write simple log file/ write the message in the console, and find the exact error first. Once you get the exact error message it is very simple to solve it. I think the error may in the calculation part of the function. So try to find the error message and fix it. If you are not able to fix after getting the error message. post your error message here.

    By Nathan
    Direction is important than speed


Sign In to post your comments