Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Between" in Linq C# [duplicate]

Tags:

c#

linq-to-sql

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'    
like image 579
LIX Avatar asked Feb 16 '10 08:02

LIX


People also ask

Can we use multiple where clause in LINQ?

A single query expression may have multiple where clauses.

How use contains in LINQ query?

All, Any & Contains are quantifier operators in LINQ. All checks if all the elements in a sequence satisfies the specified condition. Any check if any of the elements in a sequence satisfies the specified condition. Contains operator checks whether specified element exists in the collection or not.

How do I return a single value from a list using LINQ?

var fruit = ListOfFruits. FirstOrDefault(x => x.Name == "Apple"); if (fruit != null) { return fruit.ID; } return 0; This is not the only road to Rome, you can also use Single(), SingleOrDefault() or First().

What is select in LINQ C#?

The Select() method invokes the provided selector delegate on each element of the source IEnumerable<T> sequence, and returns a new result IEnumerable<U> sequence containing the output of each invocation.


1 Answers

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.

like image 82
jjnguy Avatar answered Oct 13 '22 01:10

jjnguy