2

I am trying to query a collection of records using a ">=" for a string column/member.

What I want/am expecting is that such a condition would, given the following values in the "musician" member:

Clarence "Gatemouth" Brown
Merle Travis
Eddie Van Halen
Rory Gallagher
Elvin Bishop
Eric Clapton
Jimi Hendrix
Stevie Ray Vaughan
Robin Trower
Ritchie Blackmore
Carlos Santana
Mark Knopfler
Pete Anderson

...and the following LINQ query:

private readonly List<Musician> musicians = new List<Musician>();
. . .
public IEnumerable<Musician> Get(string musician)
{
    IEnumerable<Musician> Musicians = from m in musicians
                                      where m.musician >= musician
                                      select m;
    . . .

...with "Robin" passed as the "musician" arg, return an IEnumerbale collection of Musician containing those records with a musician value of:

Robin Trower
Rory Gallagher
Stevie Ray Vaughan

(IOW, anything "greater than" Robin, such as what would appear from "Robin" onwards if using an "ORDER BY MUSICIAN" clause in a SQL query).

Instead of that working as expected, though, it doesn't even compile, and I get, "Operator '>=' cannot be applied to operands of type 'string' and 'string'" on the "where m.musician >= musician" portion of the LINQ query.

So how can I use LINQ to filter a collection of "records" by a string value?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

6 Answers6

1

You should use String.Compare(str1, str2) which returns:

  • Less than zero if str1 is smaller than str2
  • Greater than zero if str1 is bigger than str2
  • 0 it str1 equals str2

Here is the link to the documentation

    var musicians = new List<string>()
    {
        "Clarence \"Gatemouth\" Brown",
        "Merle Travis",
        "Eddie Van Halen",
        "Rory Gallagher",
        "Elvin Bishop",
        "Eric Clapton",
        "Jimi Hendrix",
        "Stevie Ray Vaughan",
        "Robin Trower",
        "Ritchie Blackmore",
        "Carlos Santana",
        "Mark Knopfler",
        "Pete Anderson",
    };

    var musician = "Robin";
    var newMusicians = musicians.Where(m => 0 < String.Compare(m,  musician));

    MessageBox.Show(String.Join(Environment.NewLine, newMusicians.ToArray()));

Or if you prefer sql-like syntax:

var newMusicians = from m in musicians
                where 0 < String.Compare(m, musician)
                select m;
Andrzej Gis
  • 13,706
  • 14
  • 86
  • 130
  • Wouldn't the where portion of the sql-like syntax have to be actually: where 0 < String.Compare(m.musician, musician) ? – B. Clay Shannon-B. Crow Raven Nov 12 '13 at 20:06
  • 1
    @ClayShannon In your case yes, indeed. But as you didn't provide your Musician class I improvised and run it on a simple list of strings. The snipped I provided compiles and gives a desired result though. With that slight modification you mentioned it'll give you exactly what you need :) – Andrzej Gis Nov 12 '13 at 20:17
1

I think you may be looking for something like the following, which finds a given substring anywhere in search string. It does not, however, search each possible substring of the search criteria string.

IEnumerable<Musician> Musicians = musicians.Where(m => m.IndexOf(musician, StringComparison.CurrentCultureIgnoreCase) >= 0);
Az Za
  • 394
  • 1
  • 9
  • 1
    This *doesn't* find substring within a string. This is looking to see if any of the characters exist, whereas a substring would be verifying that all of the characters exist in order. – Servy Nov 12 '13 at 19:36
1
var newList = myList.OrderBy(m => m).Where(m => string.Compare(m, "Robin") > 0);

where myList is your list of string values.

camainc
  • 3,750
  • 7
  • 35
  • 46
1

You can use String.Compare in your LINQ query like this:

IEnumerable<Musician> Musicians = from m in musicians
                                          where String.Compare(m.musician, musician) > 0
                                          select m;
Poornima
  • 918
  • 5
  • 11
1
var Musicians = MusicianList
          .Where(m => string.Compare(m.musician, musician) >= 0)
          .OrderBy(m=>m.musician);

returns your desired results

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
1

Replace your query with this:

IEnumerable<Musician> Musicians = from m in musicians
                                  where string.Compare(m.musician, musician, true) > 0
                                  select m;
DougEC
  • 357
  • 1
  • 6