none
"AND" text search queries? RRS feed

  • Question

  • Hey folks...

    First of all, I'm reallllllllll new so I apologize in advance for any errors in terminology or gross stupidity.  I'm working on a RIA project using L2S and have written a few very simple queries that work fine.  But now I'm trying to get a handle on doing a text search with multiple search terms.  I understand that "contains" has issues but for what I'm doing it's fine.  I have a text box in which I'l like to be able to enter mutiple terms, such as a search for "brown fox" returning "the quick brown fox..." .  Is this going to be wayyyyyyyyyyy over my head, or even possible?

    Thanks for any pointers in the right direction!

    Paul
    Monday, February 1, 2010 7:46 PM

Answers

  • Hi Paul,

    it seems you need something more in line with Dynamic LINQ.  Please review the following post:

    http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

    another good link:

    http://tomasp.net/articles/dynamic-linq-queries.aspx

    regards,
    kashif
    • Edited by Kashif Pervaiz Wednesday, February 3, 2010 6:41 AM removed white spaces
    • Marked as answer by PaulBinCT Wednesday, February 3, 2010 12:35 PM
    Wednesday, February 3, 2010 6:36 AM
  • Hi Paul,

     

    I agree with kashif that the Dynamic LINQ Library can be very helpful for such a scenario.  E.g. we have the search items “brown fox”, then the following query can help us to retrieve all the entities whose certain field contains both “brown” and “fox”. 

    ================================================================================================

                    string searchValue = "brown fox";

                    string[] searchItems = searchValue.Split(' ');

                    string searchQuery = "";

                    for (int i = 0; i < searchItems.Length - 1; i++)

                    {

                        searchQuery += ("CertinField.Contains(@" + i.ToString() + ") and ");

                    }

                    searchQuery += ("CertinField.Contains(@" + (searchItems.Length - 1).ToString() + ")");

     

                    var query = db.ParentTables.Where(searchQuery, searchItems);

    ================================================================================================

     

    The String.Contains() method can be used directly in the Dynamic query expressions.

    For a detailed guidance of Dynamic LINQ Library, please see the Dynamic Expressions.html file included in the DynamicQuery project in both C# and VB.NET VS2008 samples. 

     

    If you have any questions, please feel free to let me know.

     

     

    Have a great day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, February 3, 2010 7:48 AM
    Moderator

All replies

  • Hello Paul,

     

    Glad to see you again!

     

    Could you please provide us with more detailed information about how do you want to search the text by multiple terms?   Do you mean the certain column should contain one term and meanwhile it should contain another term?   If so, I think a WHERE query like this.DataContext.Table1.Where(t => t.Column1.Contains(“term1”) && t.Column1.Contains(“term2”); can get the results.   Although I am not an expert of Silverlight RIA Services, but I will do my best to help.   

     

    Besides, the Silverlight forum has a specific forum queue for RIA Services here, http://forums.silverlight.net/forums/53.aspx.   If the problem is more related to RIA services instead of LINQ to SQL queries, I would recommend you post the question there.  Many RIA Services experts are helping us there. 

     

    Have a great day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, February 2, 2010 1:50 AM
    Moderator
  • Hello Lingzhi!

    Thanks for the nice welcome back :)

    More precisely... I have a field of text which currently I search with a single term (word) and that works fine but I need to narrow the search by adding another search term.  For instance if I search for "fox" I would get many results, but if I search for "brown fox" (ideally entered in the same text box ala Google), I would be much more focused.  These would be multiple search terms against the same field... hope I'm explaining it adequately? 
    Tuesday, February 2, 2010 7:14 PM
  • Hi Paul,

    it seems you need something more in line with Dynamic LINQ.  Please review the following post:

    http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

    another good link:

    http://tomasp.net/articles/dynamic-linq-queries.aspx

    regards,
    kashif
    • Edited by Kashif Pervaiz Wednesday, February 3, 2010 6:41 AM removed white spaces
    • Marked as answer by PaulBinCT Wednesday, February 3, 2010 12:35 PM
    Wednesday, February 3, 2010 6:36 AM
  • Hi Paul,

     

    I agree with kashif that the Dynamic LINQ Library can be very helpful for such a scenario.  E.g. we have the search items “brown fox”, then the following query can help us to retrieve all the entities whose certain field contains both “brown” and “fox”. 

    ================================================================================================

                    string searchValue = "brown fox";

                    string[] searchItems = searchValue.Split(' ');

                    string searchQuery = "";

                    for (int i = 0; i < searchItems.Length - 1; i++)

                    {

                        searchQuery += ("CertinField.Contains(@" + i.ToString() + ") and ");

                    }

                    searchQuery += ("CertinField.Contains(@" + (searchItems.Length - 1).ToString() + ")");

     

                    var query = db.ParentTables.Where(searchQuery, searchItems);

    ================================================================================================

     

    The String.Contains() method can be used directly in the Dynamic query expressions.

    For a detailed guidance of Dynamic LINQ Library, please see the Dynamic Expressions.html file included in the DynamicQuery project in both C# and VB.NET VS2008 samples. 

     

    If you have any questions, please feel free to let me know.

     

     

    Have a great day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, February 3, 2010 7:48 AM
    Moderator
  • You guys are great!  Thanks so much, it looks like that's the ticket... :)

    Wednesday, February 3, 2010 12:34 PM
  • Lingzhi,

    Is there an updated LINQ Dynamic Query Library available for Silverlight 4/ASP.NET 4? The one at Scott Gu's link http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx has some issues. The type or namespace ReaderWriterLock and LockCookie are not found when the file is added to a project.

    I found an old link (http://forums.asp.net/t/1155377.aspx/1) from 2008 that mentions the issue that I am seeing, but have not found a solution.

    Any ideas about how to use LINQ to generate dynamic queries?

    Thanks,

    Warren


    Warren
    Saturday, November 12, 2011 1:54 AM
  • I received the following info from Rob Jacobs, which answered my question.

    I had a need for using the Dynamic Linq in my Silverlight apps so I created (reverse engineered) a version you can get here (compiled against SL 4):

    https://skydrive.live.com/?cid=8694863ba917428c#cid=8694863BA917428C&id=8694863BA917428C%21132

    I also included the extensions for IEnumerable objects as described here:

    http://blog.nitriq.com/default,month,2009-11.aspx

    These links may help others struggling with the same issue.


    Warren
    Tuesday, November 22, 2011 6:09 PM