0

I have managed to pull out the FirstorDefault() in the Query, however, some of these address types in the database actually have more than 1 address: therefore, I am trying to build an array of every address regarding each one.

The current code:

int counter = 0;
        string queryID = "";
        List<string> busAddr = new List<string>();
        while (counter != busIDs.Count)
        {
            queryID = busIDs[counter];
            var gathered = (from c in db.tblbus_address where c.BusinessID == queryID && c.AddressTypeID == addrnum select c).ToArray();
            int gath = 0;
            if ((gathered[gath] as tblbus_address) != null && !string.IsNullOrEmpty((gathered[gath] as tblbus_address).Address1))
                {
                    var address = gathered[gath] as tblbus_address;
                    string test = "";
                    while (gath != address.Address1.Length)
                    {
                        var all = address.Address1;
                        test += address.Address1.ToString() + ",";
                    }
                    busAddr.Add(test);
                }
                else
                {
                    busAddr.Add("No address for this type exists...");
                }
                counter++;
                gath++;
            }

I am receiving an error:

Index was outside the bounds of the array.

at this line:

if ((gathered[gath] as tblbus_address) != null && !string.IsNullOrEmpty((gathered[gath] as tblbus_address).Address1))

Can anyone point me in the right direction? I know this structure is the issue but I cannot think how to approach the situation.

1
  • Got to check for empty array gathered if query returned no results Commented Feb 15, 2016 at 14:22

1 Answer 1

1

You are trying to get the element gathered[gath] when there are no items in gathered. Adding a check for null and Any will help you

if(gathered!=null && gathered.Any() 
  && (gathered[gath] as tblbus_address) != null 
  && !string.IsNullOrEmpty((gathered[gath] as tblbus_address).Address1))
{
    ...
    ...
}
Sign up to request clarification or add additional context in comments.

4 Comments

That stopped my error thank-you & fixed it all! I have another issue, it seems now that instead of counting how many where in that element it just appends them all to test in the string here: while (gath != address.Address1.Length) { var all = address.Address1; test += address.Address1.ToString() + ","; } any idea to why?
Couldnt understand what you need to do. You are appending it to test += address.Address1.ToString(). You will be getting all that text. whose count are you looking for? gathered.Length you mean?
Oh, appologies. I am using that because I want an array of all the addresses that one company has per index. Then later, I will just .split(",") because I am using JavaScript front end to allow searches and stuff.
Ya, marked it :) Thank-you for that @CarbineCoder !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.