LINQ表达式用于执行" ...之间QUOT; [英] Linq expression for executing a "Between"
问题描述
在SQL您必须编写一个列的类型是nvachar'的,只是返回给大家,是指定的最小值和最大值之间的行之间执行的查询的能力。
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.
例如,
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.
有没有办法使用LINQ做到这一点还是我将不得不创建一个自定义查询,执行呢?我见过的唯一的例子包括日期或整数,这使得它非常容易(可以使用比较操作符,如<,>等)。
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).
推荐答案
您会使用的CompareTo
而不是:
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 :)
这篇关于LINQ表达式用于执行" ...之间QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!