locked
Trouble using linq in If Else statement RRS feed

  • Question

  • Im having trouble using a simple linq query to compare a date the user selects with a table of dates(restricted dates). If the date is in the table and the 'Active' bool flag is set to 'true' the date is restricted. Im using the 'true' or 'false' return in an If / Else statement to throw a message if true or call a process_request method if false. It will throw the message if true, but when false the method call look like it tries but never opens the modal window

    public void check_restriction()
            {
                Shipment s = Shipments.SelectedItem;
                bool rActive = false;
                rActive = Shipments.SelectedItem.TenantFacility.TenantFacilityTimeRestrictions.Where(d => d.startDate.Date == Shipments.SelectedItem.requestDate.Value.Date).FirstOrDefault().Active;
                if (rActive == true)
                {
                    Microsoft.LightSwitch.Presentation.Extensions.ScreenExtensions.ShowMessageBox(this"This facility is closed on the date you selected, Please try another date""Click OK to continue"MessageBoxOption.Ok);
                }
                else
                {
                    process_request();
                }
            }




    • Edited by canderson70 Friday, February 15, 2013 2:37 PM
    Friday, February 15, 2013 2:32 PM

Answers

  • Hey Justin, thank you for your response.  We figured it out, the expression worked if there was a record but failed 'Sequence contains new elements' if there was no record. We used a Try / Catch to capture the error and write it to an EventLog table.  This was a noob mistake on my part.  To fix it we switch the call to the database to use a var someVar and look for the FirstOrDefault() record. Then checked the someVar != null.  If someVar is not null then check the someVar.Active Bool flag in the record, else if someVar is null then 'process_request'.  Trying to include some details for others, hope this is helpful.  Here's the code we use to get it working.

    		Shipment s = Shipments.SelectedItem;
     
                    var someVar = s.TenantFacility.TenantFacilityTimeRestrictions.Where(d => d.startDate.Date == Shipments.SelectedItem.requestDate.Value.Date).FirstOrDefault();
     
                    if (someVar != null)
                    {
                        if (someVar.Active == true)
                        {
                            Microsoft.LightSwitch.Presentation.Extensions.ScreenExtensions.ShowMessageBox(this"This facility is closed on the date you selected, Please try another date""Click OK to continue"MessageBoxOption.Ok);
                        
                        }
                        if (someVar.Active == false)
                        {
                            process_request();
                        }
                    }
                    if (someVar == null)
                    {
                        process_request();
                    }

    Saturday, February 16, 2013 6:14 PM

All replies

  • So the problem that you are having is not with this method but with the process_request method? You need to provide its code for us to help you.

    Justin Anderson, LightSwitch Development Team

    Friday, February 15, 2013 9:14 PM
    Moderator
  • Hey Justin, thank you for your response.  We figured it out, the expression worked if there was a record but failed 'Sequence contains new elements' if there was no record. We used a Try / Catch to capture the error and write it to an EventLog table.  This was a noob mistake on my part.  To fix it we switch the call to the database to use a var someVar and look for the FirstOrDefault() record. Then checked the someVar != null.  If someVar is not null then check the someVar.Active Bool flag in the record, else if someVar is null then 'process_request'.  Trying to include some details for others, hope this is helpful.  Here's the code we use to get it working.

    		Shipment s = Shipments.SelectedItem;
     
                    var someVar = s.TenantFacility.TenantFacilityTimeRestrictions.Where(d => d.startDate.Date == Shipments.SelectedItem.requestDate.Value.Date).FirstOrDefault();
     
                    if (someVar != null)
                    {
                        if (someVar.Active == true)
                        {
                            Microsoft.LightSwitch.Presentation.Extensions.ScreenExtensions.ShowMessageBox(this"This facility is closed on the date you selected, Please try another date""Click OK to continue"MessageBoxOption.Ok);
                        
                        }
                        if (someVar.Active == false)
                        {
                            process_request();
                        }
                    }
                    if (someVar == null)
                    {
                        process_request();
                    }

    Saturday, February 16, 2013 6:14 PM
  • Just to follow up, you can write a bit of your code a little more cleanly like so:

    if (null != someVar && someVar.Active)
    {
        this.ShowMessageBox("This facility is closed on the date you selected, Please try another date", "Click OK to continue", MessageBoxOption.Ok);
    }
    else
    {
        process_request();
    }

    make sure to import (add a using) for the Microsoft.LightSwitch.Presentation.Extensions namespace.

    Justin Anderson, LightSwitch Development Team

    Saturday, February 16, 2013 9:12 PM
    Moderator
  • Since we are offering cleanup advice ...

    you can use Any in place of Where and FirstOrDefault because you are just checking for existence.  It will then simplify your tests

    if (s.TenantFacility
         .TenantFacilityTimeRestrictions
         .Any(d=>d.startDate == Shipments.SelectedItem.requestDate.Value.Date &&
              d.Active))
    {
        MessageBox.Show(....);
    }
    else
    {
        process_request();
    }


    Paul Linton

    Sunday, February 17, 2013 8:08 AM