Julius Julius - 1 year ago 236
C# Question

C# Orderby int linq

Hi I am trying order my type using this code in a method:

ApplicationServices.Prime_Material.OrderBy(x => x.Code).ToList();

But after ejecute it the type still the same, I was Reading about use .Sort but this is not a list then I can not use it.

this is the code of the type:

public class Prime_Material : BindableBase
private int _Code;
public int Code
get { return _Code; }
set { SetProperty(ref _Code, value); }

private string _Name;
public string Name
get { return _Name; }
set { SetProperty(ref _Name, value); }

public class Values_Prime_Material : ObservableCollection<Prime_Material> { }

public static class ApplicationServices
public static Values_Prime_Material Prime_Material = new Values_Prime_Material();

static ApplicationServices()

Any help is appreciated.

Answer Source

Your code does not modify the original ApplicationServices.Prime_Material; rather it returns the newly ordered list. Thus you should create a variable to collect the returned list. Like this:

var orderedList = ApplicationServices.Prime_Material.OrderBy(x => x.Code).ToList();
//use "orderedList" henceforth wherever you need an ordered list
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download