Sneha K

Sneha K

  • 1.2k
  • 527
  • 195.3k

How to Use Dictionary to sort the list in asp.net web api

Jul 1 2023 2:30 PM

I have Book list. That i want to sort in order like Publisher, AuthorfirstName ,AuthorSecondName, Title,Price using dictionary in Asp.net Web api.How to add key value pair combination here ?

My Web Api Code

 public  IEnumerable <BookViewModel> GetBook()
    {
        ISqlDBHelper sqlDBHelper = new SqlDBHelper();
        BookViewModel bvm = new BookViewModel();
        List<BookViewModel> bklst = new List<BookViewModel>();
        Dictionary<int, string> bk = new Dictionary<int, string>();
        DataTable dt = sqlDBHelper.ExecuteSelectCommand("uspBooklst", CommandType.StoredProcedure);
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow row in dt.Rows)
            {
                if (!DBNull.Value.Equals(row["UID"]))
                {
                    bvm.UID = Convert.ToInt32(row["UID"]);
                }
                if (!DBNull.Value.Equals(row["Publisher"]))
                {
                    bvm.Publisher = row["Publisher"].ToString();
                }
                if (!DBNull.Value.Equals(row["AuthorFirstName"]))
                {
                    bvm.AuthorFirstName = row["AuthorFirstName"].ToString();
                }
                if (!DBNull.Value.Equals(row["AuthorLastName"]))
                {
                    bvm.AuthorLastName = row["AuthorLastName"].ToString();
                }
                if (!DBNull.Value.Equals(row["Title"]))
                {
                    bvm.Title = row["Title"].ToString();
                }
                if (!DBNull.Value.Equals(row["Price"]))
                {
                    bvm.Price = Convert.ToDecimal(row["Price"]);
                }

                bklst.Add(bvm);
            }
        }

        return bklst;
    }

Output

 

In Api Output the order is not in the correct format which i am expecting so i plan to use dictionary but i donno how to use it in Asp.net Web api .Help me to resolve the issue.


Answers (10)