Answered Startswith()

  • Wednesday, March 26, 2008 8:20 PM
     
     

     

    I don't know how to use' like [a-g]%' syntax in linq, I know about startswith() but I want to search

     a list of name which start from A or b throght g

    please help

All Replies

  • Wednesday, March 26, 2008 8:30 PM
     
     

    If you use LINQ To Object, you can use Regex.IsMatch.

    Else, you won't be able to do it easily.

    You can do this:

    Code Snippet

    var q = from c in context.Categories

            let categoryName = c.CategoryName

            where categoryName.StartsWith("a") || categoryName.StartsWith("b") || categoryName.StartsWith("c") || categoryName.StartsWith("d") ||

                categoryName.StartsWith("e") || categoryName.StartsWith("f") || categoryName.StartsWith("g")

            select c;

     

     

  • Wednesday, March 26, 2008 8:44 PM
     
     

    well I did that way but my query is going very long. are you telling me there is no alternate syntax for 'like[a-g]%'

    in linq.

     

  • Wednesday, March 26, 2008 8:52 PM
     
     
    I am sorry but I think so.

    Note that with VB, you have LIKE operator in LINQ BUT ONLY for LINQ To Object.

  • Wednesday, March 26, 2008 9:00 PM
     
     Answered

    You can also do this:

    Code Snippet

    string[] vals = { "a", "b", "c", "d", "e", "f", "g" };

    var q = from c in context.Categories

            let categoryName = c.CategoryName

            where vals.Contains(categoryName.Substring(0, 1))

            select c;

     

     

     

  • Wednesday, March 26, 2008 9:14 PM
     
     

    That's my answere Thanks