I need to convert a var
in the type List<myStruct>
.
I had to use var
to order a list of myStruct
and I got orderedList2
, but now I need to iterate this list and I don't know how to do it.
public struct myStruct
{
public String delivery;
public String articleCode;
public String dex;
public String phase;
public String quantity;
};
List<myStruct> myList;
var orderedList2 = myList.OrderByDescending(x =>
{
DateTime dt;
DateTime.TryParse(x.delivery, out dt);
return x;
});
// now I have to fill the ListView again
foreach(myStruct str in orderedList2)
{
string[] s = new string[5];
s[0] = str.delivery;
s[1] = str.articleCode;
s[2] = str.dex;
s[3] = str.phase;
s[4] = str.quantity;
ListViewItem itl = new ListViewItem(s);
////String s = r["DEXART"].ToString();
////MessageBox.Show(s);
listView1.Items.Add(itl);
}
When the code hits the foreach
statement I get an exception.
Either implement IComparable on your struct or you have a mistake in your order lambda:
var orderedList2 = myList.OrderByDescending(x =>
{
DateTime dt;
if( DateTime.TryParse(x.delivery, out dt))
return dt;
return DateTime.MinValue;
});
you can follow it with .ToList()
cal to get List<myStruct> orderedList2
.
it works: