21 September 2015

How to convert dataset to KeyValuePair using Linq

Here is an example
 
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Autocomplete3(string term)
        {
            string cnnStr = ConfigurationManager.ConnectionStrings["GraphConnection"].ConnectionString;
            SqlConnection dcnn = new SqlConnection(cnnStr);
            SqlCommand dcmd = new SqlCommand("dbo.GetCountry", dcnn);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = dcmd;
            DataSet ds = new DataSet();
            da.Fill(ds);

            List> cty = ds.Tables[0].AsEnumerable().Select(row => new KeyValuePair((string)row["CountryCode"], (string)row["CountryName"])).ToList();

            return Json(cty, JsonRequestBehavior.AllowGet);
        }

No comments:

Post a Comment