“Between” in Linq C# [duplicate]

做~自己de王妃 提交于 2020-06-25 09:13:32

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!