问题
Possible Duplicate:
LINQ Between Operator
Dear All,
Hi,
I need to write this query in LINQ C#. can anyone help me?
Select *
From Mytable
where MyText BETWEEN 'john' AND 'Pear'
回答1:
I believe this query should work:
var results = yourTable.Where(x => x.Text.CompareTo("john") > 0 &&
x.Text.CompareTo("Pear") < 0);
This assumes that you want to compare the text in each row of the table, and not some pre-dfined string.
回答2:
Here is how you can do it with ObjectQuery
MytableSet.Where("it.Name between @start and @end", new ObjectParameter("start", "john"), new ObjectParameter("end", "Pear"))
EDIT:
Forget to mention that this statement is specific to Entity Framework not LINQ2SQL.
来源:https://stackoverflow.com/questions/2271509/between-in-linq-c-sharp