Linq Get items higher then lastname

2019-02-28 03:39发布

问题:

Greetings

I'm trying to find a way using Linq-to-SQL to get the first 15 records which have a higher last name then "Jan".

When I use this in SQL I get every member which lastname starts with any letters higher then "Jan" on alphabetical order.

However using query.Where(m=>m.LastName > "Jan") does not work sadly.

Anyone know how I can achieve this ?

回答1:

You could try:

query.Where(m => m.LastName.CompareTo("Jan") > 0)

That's how you'd write it in normal C#, after all.

It's not clear whether this is LINQ to Objects or some other provider. If it's LINQ to Objects, you should consider using an explicit StringComparer. For example:

query.Where(m => StringComparer.CurrentCulture.Compare(m.LastName, "Jan") > 0)

this makes it clear what kind of comparison you want. If you're using something like LINQ to SQL, that may not work - I would imagine you'll get whatever sort of comparison the provider can handle, basically.



回答2:

query.Where(m => m.Substring(0, 3).CompareTo("Jan") > 0);