Reputation: 103
I read similar questions and responses on this issue, and tried a number of different ways to do this, but was not successful. The records show up in the datagrid in random order, not ordered by last name. Thank you in advance.
MyEntities theEntities = new MyEntities();
IEnumerable<NameTable> nameitems = (from myrow in theEntities.NameTable.AsEnumerable()
orderby myrow.NameID
select new NameTable
{NameID = myrow.NameID,
FirstName = myrow.FirstName,
LastName = myrow.LastName});
ObservableCollection<NameTable> nameitemsOC = new ObservableCollection<NameTable>(nameitems);
return nameitemsOC;
Upvotes: 0
Views: 1239
Reputation: 1995
You sort by NameID
and not LastName
.
Also of what type is theEntities.NameTable
? Maybe you can just create a CollectionView
, add a SortDescription
and make this to be the ItemsSource
of your DataGrid
.
ICollectionView myCollectionView = CollectionViewSource.GetDefaultView(theEntities.NameTable);
myCollectionView.SortDescriptions.Add(new SortDescription("LastName", ListSortDirection.Ascending));
Upvotes: 2
Reputation: 9271
I think this should do
IEnumerable<NameTable> nameitems = (from myrow in theEntities.NameTable.AsEnumerable()
orderby myrow.LastName
select new NameTable {
NameID = myrow.NameID,
FirstName = myrow.FirstName,
LastName = myrow.LastName
});
Upvotes: 2
Reputation: 102793
That's probably because you're ordering by NameID instead of LastName... try this:
orderby myrow.LastName
Upvotes: 3