Linq Get items higher then lastname

2019-02-28 03:23发布

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 ?

2条回答
\"骚年 ilove
2楼-- · 2019-02-28 04:14

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.

查看更多
仙女界的扛把子
3楼-- · 2019-02-28 04:15
query.Where(m => m.Substring(0, 3).CompareTo("Jan") > 0);
查看更多
登录 后发表回答