c# var, linq

ITsolutionWizard
ITsolutionWizard used Ask the Experts™
on
The codes below is working ok. I just need to make VAR response to be sorted.
How can I do that?

   public static List<Make> GetVehicleMakeList()
        {
            List<Make> lstMake = new List<Make>();
            HttpClient objClient = new HttpClient();
            try
            {
                var response = objClient.GetStringAsync(Utility.Enumeration.vehicleMakeAPICall).Result;
                if (!string.IsNullOrEmpty(response))
                {
                    VehicleMakeAPIResponse objMakeResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<VehicleMakeAPIResponse>(response);
                    return objMakeResponse.Results;
                }

            }
            catch (Exception ex)
            {

                //log error message here
            }
            return lstMake;
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer
Top Expert 2009
Commented:
Assuming you have to sor the result use linq order by

for example ascending order:
return objMakeResponse.Results.OrderBy(s => s.YourProperty);

Open in new window

Author

Commented:
can you help me with using my codes?

I tried below and got alert. by the way, i would like to do sort (make_name) asc.

 return objMakeResponse.Results.OrderByDescending(s => s.Make_Name);
Fernando SotoRetired
Distinguished Expert 2017
Commented:
Hi ITsolutionWizard;

Depending on what kind of collection is being held by var response you may need to create an instance of a custom comparer to use with the OrderBy method. Please see the documentation below to see how to implement the OrderBy method.

Enumerable.OrderBy Method
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Fernando SotoRetired
Distinguished Expert 2017

Commented:
And what did the alert state?
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Your method returns a List<Make>, but OrderByDescending returns an IOrderedEnumerable. You'll want to either call ToList after OrderByDescending or change your method signature.

Author

Commented:
can you show me how with my codes?
Fernando SotoRetired
Distinguished Expert 2017

Commented:
In order to help you please tell us what is the data type being returned by the following statement?
var response = objClient.GetStringAsync(Utility.Enumeration.vehicleMakeAPICall).Result

Open in new window

Author

Commented:
List strong
Fernando SotoRetired
Distinguished Expert 2017

Commented:
List<XXX>, list of what, what object type is XXX? Please also post the class XXX.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial