“Between” in Linq C# [duplicate]

2020-08-26 03:30发布

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'    

2条回答
唯我独甜
2楼-- · 2020-08-26 04:00

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.

查看更多
时光不老,我们不散
3楼-- · 2020-08-26 04:17

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.

查看更多
登录 后发表回答