Sort list by eventually existing value
I have to sort a given list by a value which may or may not be given
inside another list of each element. If no value is given, this element
should appear at the bottom of the sorted list.
Here's a short example: I want to sort the list Items based on the Value
of the Property which has the name foo
public class Item {
public string Name { get; set; }
public List<Property> Properties { get; set; }
}
public class Property {
public string Name { get; set; }
public int Value { get; set; }
}
List<Item> items = new List<Item>() {
new Item() {
Name = "Item 1",
Properties = new List<Property>() {
new Property {
Name = "foo",
Value = 5
},
new Property {
Name = "bar",
Value = 7
}
}
},
new Item() {
Name = "Item 2",
Properties = new List<Property>() {
new Property {
Name = "bar",
Value = 2
}
}
},
new Item() {
Name = "Item 3",
Properties = new List<Property>() {
new Property {
Name = "foo",
Value = 1
}
}
}
The sorted list should contain the Items in the order Item 1, Item 3, Item 2
I tried this:
items.FetchOrderBy
(
x => x.Properties
.FirstOrDefault
(
y => y.Name = "foo"
)
.Value
)
.ToList();
...But got the following exception: Antlr.Runtime.NoViableAltException
No comments:
Post a Comment