locked
LINQ equivalent to '%%' RRS feed

  • Question

  • If I have a query with a where clause

          DarkMattersEntities LINQ = new DarkMattersEntities();
          var results = from a in LINQ.contacts
                 where a.ccode == ccode
                 select new search_()
                 {
                   sysid = a.sysid,
                   FullName = a.full_name,
                   ClientNum = a.con_no,
                   BillingSup = a.title,
                   Main_Phn = a.phone1,
                   Address = a.con1_02_03,
                   City = a.con1_02_05,
                   State = a.con1_02_06,
                   Zip = a.con1_02_07,
                   SetTerms = a.con1_05_05,
                   IntTerms = a.con1_05_08
                };
          DataSet = results;
    but I want to pass in a value to let the value be non selective how would I do this without writing a whole new method ?

    Monday, March 21, 2011 1:35 PM

Answers

  • Another way to make the same is using e-sql. There is an advertisment however . If you write a command with a syntax error, the error doesn't appear until the code will be executed.
    You can find more information about e-sql language at this url: http://msdn.microsoft.com/en-us/library/bb387118.aspx

    I am posting a console application sample to show you how to use e-sql.

    Regards.

        class Program
        {
            static void Main(string[] args)
            {
                AdventureWorks2008R2Entities e1 = new AdventureWorks2008R2Entities();

                var personList=e1.CreateQuery<string>("select value p.FirstName from person as p where p.FirstName like '__ac%'");
                foreach (string name1 in personList)
                {
                    Console.WriteLine(name1);
                }

            }
        }


    Gustavo García MCPD
    • Marked as answer by Jackie-Sun Monday, March 28, 2011 3:00 AM
    Tuesday, March 22, 2011 2:10 PM
  • Hi d_rop4nme;

    If this is Linq to SQL you can use the  SqlMethods.Like(...) to be able to use the T-SQL Like method in your linq query. The other metacharacters that can be used can be found at LIKE (Transact-SQL).

    So if you are looking for a ccode that start with 854 you can do this:
    where SqlMethods.Like(a.ccode, "854%")
    Or end with 854
    where SqlMethods.Like(a.ccode, "%854")
    Or has 854 anywhere in it
    where SqlMethods.Like(a.ccode, "%854%")

    To use SqlMethods you will need to add the following namespace:

    using System.Data.Linq.SqlClient;

    Fernando

     

     
    If a post answers your question, please click "Mark As Answer" on that post and "Mark
    • Marked as answer by Jackie-Sun Monday, March 28, 2011 3:00 AM
    Tuesday, March 22, 2011 2:14 PM

All replies

  • Hi

    You can use the following methods

    a.ccode.StartsWith("XXX") if you want to obtain a resultset with a predicate like  "XXX%"
    a.ccode.EndsWith("XXX") if you want to obtain a resultset with a predicate like  "%XXX"
    a.ccode.IndexOf("XXX")==2 if you want to obtain a resultset with a predicate like  "__XXX%"

    I hope this response will be useful for you.

    Regards


    Gustavo García MCPD
    Monday, March 21, 2011 8:03 PM
  • Another way to make the same is using e-sql. There is an advertisment however . If you write a command with a syntax error, the error doesn't appear until the code will be executed.
    You can find more information about e-sql language at this url: http://msdn.microsoft.com/en-us/library/bb387118.aspx

    I am posting a console application sample to show you how to use e-sql.

    Regards.

        class Program
        {
            static void Main(string[] args)
            {
                AdventureWorks2008R2Entities e1 = new AdventureWorks2008R2Entities();

                var personList=e1.CreateQuery<string>("select value p.FirstName from person as p where p.FirstName like '__ac%'");
                foreach (string name1 in personList)
                {
                    Console.WriteLine(name1);
                }

            }
        }


    Gustavo García MCPD
    • Marked as answer by Jackie-Sun Monday, March 28, 2011 3:00 AM
    Tuesday, March 22, 2011 2:10 PM
  • Hi d_rop4nme;

    If this is Linq to SQL you can use the  SqlMethods.Like(...) to be able to use the T-SQL Like method in your linq query. The other metacharacters that can be used can be found at LIKE (Transact-SQL).

    So if you are looking for a ccode that start with 854 you can do this:
    where SqlMethods.Like(a.ccode, "854%")
    Or end with 854
    where SqlMethods.Like(a.ccode, "%854")
    Or has 854 anywhere in it
    where SqlMethods.Like(a.ccode, "%854%")

    To use SqlMethods you will need to add the following namespace:

    using System.Data.Linq.SqlClient;

    Fernando

     

     
    If a post answers your question, please click "Mark As Answer" on that post and "Mark
    • Marked as answer by Jackie-Sun Monday, March 28, 2011 3:00 AM
    Tuesday, March 22, 2011 2:14 PM