开发者

Cannot implicitly convert type string to string[] in autocomplete method

开发者 https://www.devze.com 2023-01-11 05:36 出处:网络
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.

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 inh开发者_开发问答ouse = 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.


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.


The last line should be:

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消