开发者

Linq expression for executing a "Between"

In SQL you have the ability to write a query that executes a between on a column that is of type 'nvachar' and simply returns to you all the rows that are between the min and max values specified.

For Example,

Table (Id:Int, Name:nvarchar):

Contents:
1, Annie
2, Bill
3, Frank
4, Phil
5, Ted

Select * where Name Between 'Frank' and 'Ted'

Should return Frank, Phil, and Ted开发者_如何学Go.

Is there a way to do this with linq or am I going to have to create a custom query and execute it? The only examples I have seen involve dates or integers which make it very easy (can use the comparison operators like <, > etc).


You'd use CompareTo instead:

var query = from name in names
            where name.CompareTo("Frank") >= 0 &&
                  name.CompareTo("Ted") <= 0
            select name;

Use > and < to be exclusive (i.e. to exclude Frank and Ted).

Basically it's the same as using < and >, but with methods :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜