不允许自动完成中的重复值

时间:2016-12-21 06:56:46

标签: c# asp.net dotnetnuke

我使用此jquery进行自动完成。它工作正常,但我不想显示重复值。 我该怎么办?

<script type="text/javascript">
    $(document).ready(function () {
        $("[id*=txtSearch1]").autocomplete({ source: '<%=ResolveUrl("~/DesktopModules/styPDashboard/Handler.ashx" ) %>' });
    });
</script>

STY,[21.12.16 10:40] public class Handler:IHttpHandler {

public void ProcessRequest(HttpContext context)
{
    string prefixText1 = context.Request.QueryString["term"];
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand())
        {

            cmd.CommandText = "select CompName from styComp_Profile where CompName like @txtSearch1 + '%'  ";
            cmd.Parameters.AddWithValue("@txtSearch1", prefixText1);
            cmd.Connection = conn;
            List<string> styComp_Profile = new List<string);
            conn.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    styComp_Profile.Add(sdr["CompName"].ToString());
                }
            }
            conn.Close();
            context.Response.Write(new JavaScriptSerializer().Serialize(styComp_Profile));
        }
    }
}

2 个答案:

答案 0 :(得分:0)

int [] array1 = {1,2,2,3,4,4};

var result = array1.Distinct();

只会产生不同的元素。

答案 1 :(得分:0)

根据您发布的代码。您只需将DISTINCT添加到命令

即可
cmd.CommandText = "select distinct CompName from styComp_Profile where ...";