Reputation: 48550
I have a class and I am creation its collection like this
class Foo
{
public int ID { get; set; }
// ID will remain unique in collection
public string Name { get; set; }
}
List<Foo> sc = new List<Foo>();
sc.Add(new Foo()
{
ID = 0,
Name= "Michael"
});
sc.Add(new Foo()
{
ID = 2,
Name= "Natasha"
});
sc.Add(new Foo()
{
ID = 1,
Name= "Casandra"
});
Then I get a DataTable
DataTable dt = GetDataTableMethod();
Datatable contains only name and not ID and will always have names less than collection. (bcoz collection is master collection)
Now I want to sort my datatable based on "name"
but based on sorting of Foo Class collection which will be based on id.
Result after sorting datatable should be
Michael
Casandra
Natasha
I tried
var v = dt.Rows.Cast<DataRow>().OrderBy(What should i write here?);
Upvotes: 3
Views: 241
Reputation: 12525
As far as I understood your question, you have collection which has data including ids and stored procedure that returns DataTable
containing only one of its component. The simplest way is to iterate through list of data rows and match respective value from the full list and order after that:
List<Foo> sc = new List<Foo>();
sc.Add(new Foo()
{
ID = 0,
Name = "Michael"
});
sc.Add(new Foo()
{
ID = 2,
Name = "Natasha"
});
sc.Add(new Foo()
{
ID = 1,
Name = "Casandra"
});
List<string> dt = new List<string>(); //For testing replaced just by string.
dt.Add("Michael");
dt.Add("Natasha");
dt.Add("Casandra");
var ordered = dt.Select(i => sc.First(c => c.Name == i)).OrderBy(i => i.ID);
Demo (sorry for awful formatting).
Upvotes: 2