asp.net - LINQ sorting, doesn't work -
I have a LINQ query like this:
to i _db.Items.OfType And in lt; MP. M in M. module. P in pages I. = Null & amp; Amp; I.Type == 1 & amp; Amp; I.Published == True & amp; Amp; P.PageId == 2 Select P
I use the query in this way because I have a strongly typed view (ASP.NET MVC).
Sorted by item i.Sort
property. orderby i.Sort
and i.orderby (this => it.Sort)
does not work. How can I fix this?
When you sort with Linq you typically call OrderBy
Property, and ultimately a icon
, not a sorting function. For example:
class person {get public ent era; Set;}} public static zero main () {var ps = new list & lt; Person & gt; (); Ps.Add (new person {age = 1}); Ps.Add (new person {age = 5}); Ps.Add (new person {age = 3}); Var Sorted = ps.OrderBy (p = & gt; p.Age); Foreach (sorted P) {Console.WriteLine (p.Age); }}
Here Linq will know how to sort integers like the right way.
And without context (such as i.Sort
, what is the purpose of this, what you want to do with it), it would be difficult to be more specific for your problem .
However, I'm pretty sure that you are wrong OrderBy
: You should give it a lambda expression that recognizes properties of the objects located in your sequence , And then Linq will sort your sequence according to the normal order of that type of property (or according to another order you define for that type, using IComparer).
Comments
Post a Comment