user1405195
user1405195

Reputation: 1681

Casting IOrderedEnumerable to ICollection

I want to sort the order of some random integers before inserting into my database.

if (integerList.Direction.Equals("ascending")) {
    integerList.Integers = integerList.Integers.OrderBy(i => i.IntegerValue);
} else {
    integerList.Integers = integerList.Integers.OrderByDescending(i => i.IntegerValue);
}

orderby and orderbydescending seem to convert the integers from ICollection in my class to IOrderedEnumerable. Can anybody suggest how to cast the sorted integers?

Upvotes: 3

Views: 3078

Answers (2)

pescolino
pescolino

Reputation: 3123

From your question I assume that the Integers property is of type ICollection<int>. Add ToList() or ToArray() to convert the result into a List<int> or an int[] (both implement ICollection).

Since you can not be sure that the database stores the values in the order in which they are added it does not make sense to order them before adding to the database. Instead you should add an index to the respective field and use ORDER BY on that field to get ordered values back.

Upvotes: 0

ywm
ywm

Reputation: 1127

ToList()

Will create a List for your, if I remember correctly that inherits from Collection.

Upvotes: 7

Related Questions