using LINQ and like together

Last post 05-19-2008 12:52 AM by bullrout@gmail.com. 4 replies.

Sort Posts:

  • using LINQ and like together

    05-13-2008, 9:17 PM

     Hi There,

     I would like to use linq and multiple like statements. I'm really new to LINQ and i'm unsure of the syntax. Can  someone please show me how to do this?

     

    Sean - thanks in advance
     

     

     return db.Movies
              .Where(m => SqlMethods.Like(m.StoreName,prefixText)  || SqlMethods.Like(m.StoreName,prefixText) )
                .OrderBy( m => m.Title )
                .Select( m => m.Title)
                .Take(count)
                .ToArray();

  • Re: using LINQ and like together

    05-16-2008, 1:15 AM

    Hi Bullrout,

    When you want to implement the LIKE function as in sql script in LINQ, you can achieve it by following two ways.

     

    First, you can use Contains, StartsWith, and EndWith to convert it. You can take a try at it.

    Second, you can use SqlMethods class to implement it. Don't forget to refer the namespace "System.Data.Linq.SqlClient" before you use it. Here's a sample for your issue. Hope it helps you!

                          var dd = (from p in ctx.Users
                          where SqlMethods.Like(p.userName, "%Jiang%")
                          orderby p.accountID
                          select p).Take(4);

     

    Thanks.

    Sincerely,
    JanIvan Qian

    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
  • Re: using LINQ and like together

    05-16-2008, 11:34 PM

    Hi Wencui,

    Thanks for answering the question for me. I have a few more regarding the example that you posted. is this syntax legal? Also does the "Take(4);" clause mean display the first four rows only?

    SqlMethods.Like(p.userName, "%Jiang%" || p.firstName, "%Sean%")

     Sean

  • Re: using LINQ and like together

    05-18-2008, 10:31 PM
    Answer

    Hi Bullrout,

    The statement is like this where SqlMethods.Like(p.userName, "%Jiang%") || SqlMethods.Like(p.email,"%WWW%")  OR  where SqlMethods.Like(p.userName, "%Jiang%") && SqlMethods.Like(p.email,"%WWW%"). 

    P.S. Take(4) clause is used to Take/Retrive the first four rows in all the records from database.

     

    Thanks.

    Sincerely,
    JanIvan Qian

    Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
  • Re: using LINQ and like together

    05-19-2008, 12:52 AM

     Thanks for helping with this.

     

    Sean 

Page 1 of 1 (5 items)