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
{
ISqlDBHelper sqlDBHelper = new SqlDBHelper();
BookViewModel bvm = new BookViewModel();
List
Dictionary
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.
Prasad RaveendranPosted Jul 1, 2023, 3:49 PM
I have provided a sample code to use Dictionary instead of List.
Prasad RaveendranPosted Jul 3, 2023, 7:23 PM
I have updated another sample code with column sort. please have a look and confirm
Sneha KPosted Jul 2, 2023, 2:10 PM
Hi Prasad
Let me check you solution and let you know
Prasad RaveendranPosted Jul 2, 2023, 12:38 PM
Please take a look at the sample attached. I believe, its sorting as expected. you are expecting the sorting as below.
Sort by Publisher, Author last name, authorfirst name then title. as you are starting the sort with Publisher first, always the records will be displayed based on the Publisher.
if you are satifisifed with this result, please do let me know the expected output.
Prasad RaveendranPosted Jul 2, 2023, 10:49 AM
did you try my first sample code where I have included the sorting on List?
Sneha KPosted Jul 2, 2023, 7:06 AM
Hi Prasad This is my table structure
I want to write the api method to return a sorted list of these by Publisher, Author last name, authorfirst name then title. And yesterday i cleared the error but still i didnt get the values in the sorted order
Prasad RaveendranPosted Jul 1, 2023, 6:54 PM
Please use the modified method as give me below and let me know if you are facing the issue
I would request you to share the sample data so that I can take a look. I would request you share your expected output as well.
Sneha KPosted Jul 1, 2023, 5:57 PM
Hi Prasath
Still the order is not formed as per my expectation Please look below image
Sneha KPosted Jul 1, 2023, 5:36 PM
Hi Prasad
Thanks for the solution currently i am getting one error .Please check below
Prasad RaveendranPosted Jul 1, 2023, 3:45 PM
I have refactored the code as below
I simplified the null value checks by utilizing the GetValueOrDefault method and removed the redundant ToString() calls
In the refactored code:
BookViewModelobjects, I use the LINQOrderByandThenBymethods to sort the list based on the specified properties in the desired order.OrderBymethod is used to sort the list by thePublisherproperty.ThenBymethod is used to perform subsequent sorting on theAuthorFirstName,AuthorLastName,Title, andPriceproperties.ToListmethod.Please do let me know your thoughts.
I'm wondering the why you need to Dictionary for sorting? Please provide more details on this