1

In my LinQ query I have to search words that start from H to L. This is my code:

var Artists = context.GetTable<Artist>();
var leftOuterJoinQuery =
            from arti in Artists
            where arti.Name.StartsWith("H")//to L

var Artists contains table with all artists from my Database.

Someone knows how resolve this?

Sergio M.
  • 88
  • 1
  • 7

2 Answers2

1

you could have a list of letters and then filter on that - e.g:

var letters = new[] {"H","I", "J", "K", "L"};
var Artists = context.GetTable<Artist>().Where (a => letters.Any (l => a.Name.StartsWith(l)));
NDJ
  • 5,189
  • 1
  • 18
  • 27
1

In SQL you can use relational operators for this (>, <, etc.), but C# doesn't accept stringA > stringB. Fortunately, EF translates String.CompareTo into relational operators, so you can do:

where arti.Name.CompareTo("H") >= 0 && c.Name.CompareTo("M") < 0)

This will be translated into

WHERE ([Extent1].[Name] >= 'H') AND ([Extent1].[Name] < 'M')

Note that it depends on the database collation of the Name column whether or not case sensitivity is accounted for.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291