views:

35

answers:

3

Afternoon all - it's Friday th 13th so of course I am having an absolute mare!!!

The code below, 'should' create a list of items that will be used in a textbox autocomplete.

public string[] GetAutoComplete(string prefixText, int count)
    {
        try
        {
            string memberid = HttpContext.Current.Session["VDS_MemberID"].ToString(); 
            string locationid = HttpContext.Current.Session["VDS_LocationID"].ToString();
            string inhouse = HttpContext.Current.Session["VDS_Inhouse"].ToString();
            string supplier = HttpContext.Current.Session["VDS_Supplier"].ToString();
            string groupw = HttpContext.Current.Session["VDS_Group"].ToString();
            string external = HttpContext.Current.Session["VDS_External"].ToString();

            VDSORDAL.PDC_VDSOREntities autocomplete = new VDSORDAL.PDC_VDSOREntities();

            var r = (from p in  autocomplete.tblAutoCompletes
                    where p.MemberId == memberid && p.LocationId == locationid && p.ACItem.Contains(prefixText)
                    select p);

            if (inhouse != "DoNotDisplayInhouse")
                         r = r.Where(p => p.ACItem == inhouse);

            if (supplier != "DoNotDisplaySupplier")
                r = r.Where(p => p.ACItem == supplier);

            if (groupw != "DoNotDisplayGroup")
                r = r.Where(p => p.ACItem == groupw);

            if (external != "DoNotDisplayExternal")
                r = r.Where(p => p.ACItem == external);

            return r.Distinct().OrderBy(p => p.ACItem).ToString();

        }

However, I am getting the question title as an error.

Can anyone suggest a way around this? Apologies..I am having a bad day.

+1  A: 

Possibly

return r.Distinct().OrderBy(p => p.ACItem).ToString();

should be

return r.Distinct().OrderBy(p => p.ACItem).ToArray(); 

Update:

Sounds like that's your real problem. Try (following code is brain-compiled)

return (from p in r orderby p.ACItem desc select p.ACItem).ToArray();

I'm assuming ACItem is the string you want to return, if not, select what you want in the array.

Or possibly

return (from p in r where p != null orderby p.ACItem desc select p.ACItem).ToArray();

the where p != null may be necessary, you pretty much need to inspect r and see what's in there, not really enough info to be able to answer this conclusively.
That said, .ToArray instead of .ToString is still the answer to your problem, anything else is a different question.

Binary Worrier
I've tried ToArray but then received a System.ArgumentNullException Error: Cannot implicitly converty type 'VDSORDAL.tblAutoComplete[]' to 'string[]'
Ricardo Deano
OrderBy(...).Select(p => p.ToString()).ToArray();
DonaldRay
+1  A: 

The last line should be:

return r.Distinct().OrderBy(p => p.ACItem).ToArray();
Lee
A: 

Don't do a ToString(). Use either ToArray() or ConvertAll<string>().ToArray()

chilltemp