none
Getting a count of query results RRS feed

  • Question

  • I'm trying to verify a zip code in LightSwitch 2012 using a zip code (postal code) table. I tried the following code:

    partial void PostalCode_Validate(EntityValidationResultsBuilder results)
    {
               
        int count = (from postalCode in this.DataWorkspace.HHDemographicsData.PostalCodes
            where postalCode.PostalCode1 == this.PostalCode
                && City == this.City
                && County == this.County
                && StateProvinceAbbrev == this.StateProvinceAbbrev
            select postalCode.PostalCode1).Count();
               
        if (count == 0)
        {
            results.AddPropertyError("Postal Code not found in Postal Code table.");
        }
       
    }

    but found the Count function unsupported in this instance. What is the syntax I should use to accomplish this?

    Tuesday, September 4, 2012 9:23 PM

Answers

  • This code ended up working:

    partial void PostalCode_Validate(EntityValidationResultsBuilder results)
    {
        PostalCode postalCode = (from PostalCode postalCodes
            in this.DataWorkspace.HHDemographicsData.PostalCodes
            where postalCodes.PostalCode1 == this.PostalCode
            && City == this.City
            && County == this.County
            && StateProvinceAbbrev == this.StateProvinceAbbrev
            select postalCodes).FirstOrDefault();
        if (postalCode == null)
        {
            results.AddPropertyError("Postal Code not found in Postal Code table.");
        }
    }

    Wednesday, September 5, 2012 3:36 AM

All replies

  • Try adding at .Execute() before .Count().

    E.g. ...select postalCode.PostalCode1).Execute().Count();


    PP

    Wednesday, September 5, 2012 12:58 AM
  • I got the message:

    ArgumentException was unhandled by user code. Selecting the source element is the only supported selector. Looks like there's an argument mismatch.

    Wednesday, September 5, 2012 3:18 AM
  • Hi Russell

    Here is one more way to do this. Change your PostalCode data type to string and write the following code within the property validation method.

    partial void PostalCode_Validate(EntityValidationResultsBuilder results)
            {
                // results.AddPropertyError("<Error-Message>");
                if (!string.IsNullOrEmpty(this.PostalCode))
                {
                    if (this.PostalCode.Length != 6)
                    {
                        results.AddPropertyError("Please enter a valid Indian Postal Code. (ex. 110005)");
                    }
                }
            }

    Note: Here my Postal code length must have a 6 digit.

    Hope this helps...

    Regards



    Rashmi Ranjan Panigrahi
    www.lightswitchspecial.com

    If you found this post helpful, please “Vote as Helpful”. If it answered your question, please “Mark as Answer”.
    This will help other users to find their answer quickly.

    Wednesday, September 5, 2012 3:29 AM
  • This doesn't verify that the postal code is a specific value, only that it has 6 digits
    Wednesday, September 5, 2012 3:32 AM
  • This code ended up working:

    partial void PostalCode_Validate(EntityValidationResultsBuilder results)
    {
        PostalCode postalCode = (from PostalCode postalCodes
            in this.DataWorkspace.HHDemographicsData.PostalCodes
            where postalCodes.PostalCode1 == this.PostalCode
            && City == this.City
            && County == this.County
            && StateProvinceAbbrev == this.StateProvinceAbbrev
            select postalCodes).FirstOrDefault();
        if (postalCode == null)
        {
            results.AddPropertyError("Postal Code not found in Postal Code table.");
        }
    }

    Wednesday, September 5, 2012 3:36 AM