none
Soooo close... Dynamic LINQ Query help? RRS feed

  • Question

  • OK, I should at least get credit for perseverance I think... after 8 solid hours I'm so close I can smell it but not quite there.  Surely someone can give me a push over the finish line?
     
    I'm trying to write a query that will take x numbers of search terms, and build a query against one field of a table and return the results.  This is what I've got:

    public

     

    IQueryable<News_Item> SearchNews (string searchNewsTxtBox)

     

    {

     

     

    string searchValue = searchNewsTxtBox;

     

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

     

    string searchQuery = "";

     

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

     

    {

     

    searchQuery += (

    "(n.Articles.Contains"+(searchItems.ElementAt(i).ToString())+"&&");

     

    }

     

    searchQuery += (

    "(n.Articles.Contains)"+(searchItems.ElementAt((searchItems.Length)-1).ToString()));

     

     

     

    IQueryable<News_Item> query = from n in DataContext.News_Items

     

    where searchQuery

     

    select n;

     

    return query;

     

    }

    But, I have a type mismatch at "searchQuery" where it's a string and LINQ wants a Boolean... what do I do?

    Sorry, I'm sure you guys are tired of my pleas for help... :(
    But thanks!

    Paul

    Thursday, February 4, 2010 12:23 AM

Answers

  • Why oh why didn't I get "PredicateBuidler" 3 days ago???

    Solved the whole problem in under an hour... they should charge 100 bucks for it...

    Thank you thank you thank you...
    • Marked as answer by PaulBinCT Thursday, February 4, 2010 9:08 PM
    Thursday, February 4, 2010 9:08 PM

All replies

  • Hello Paul,

     

    Oh, I also noticed this thread that you posted more than 10 hours ago.  Sorry I cannot reply earlier since I am still in bed at that time.  But I am coming!  J

     

    Have you downloaded the VS2008 C# samples?   The Dynamic LINQ Library is inside the C# samples (\LinqSamples\DynamicQuery directory).  Have you found it?   In the directory, we can find a .html file named Dynamic Expressions.html which is the tutorial of the Dynamic LINQ Library.  In the DynamicQuery project, we can find C# code file Dynamic.cs.  It is the of the Dynamic LINQ Library.   Import this file to your existing project and then add the namespace “using System.Linq.Dynamic;”

     

    Then can you use my code sample in this thread, http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/872a4cb2-2447-4b97-83c5-3c0e7e8f9958?

     

    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.
    Thursday, February 4, 2010 2:22 AM
    Moderator
  • LOL... no worries Lingzhi, I know you have better things to do than babysit me ;)

    I tried your code suggestion many hours ago, and it seemed to have a couple of issues.  The code I worked up seems (please forgive my ignorance) seems to parse my search terms and then build my query pretty well but I can't figure out how to take the last step of casting it in a way that makes LINQ happy...
    Thursday, February 4, 2010 2:46 AM
  • Hi Paul,

     

    Oh, I see.   So the extension method of WHERE is working fine to get the result, right?   LINQ query is actually parsed into LINQ extension methods by C# or VB.NET compilers.   Sometimes, we cannot use all the operators in the LINQ query, like .Max(), .Min().   The current dynamic Where extension method is also one of them.   There are no corresponding LINQ operators to represent these extension methods.   So, the query should be like this one:

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

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

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

    And the “from n in db.New_Items where…” won’t pass the compiler’s check. 

     

     

    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.
    Thursday, February 4, 2010 3:01 AM
    Moderator
  • Hi Lingzhi, and thank you!

    Remember when I said I didn't want you to babysit me?  Well, I take it back ;)  I'm sorry but obviously there's some aspect of this that I just don't get.  While I haven't been able to run my test, based on my earlier experiments, it should return (for a search string of "brown" and "fox) a string like "((n.article.contains brown)&&(n.article contains fox))" for my "searchQuery" which I was attempting to pass into my where statement... clearly that's not working ;)

    So, if I understand you correctly I should rewrite:

    IQueryable<News_Item> query = from n in DataContext.News_Items where searchQuery select n; 

     

    return

    query;


    To read:

    var query = DataContext.News_Items.Where(article.contains, (brown && Fox));


    I apologize for taking so much of your time!

    Best Regards!

    Paul

    Thursday, February 4, 2010 1:52 PM
  • Lingzhi... If you're out there :S

    I tried your proposed code again, and it throws this exception:

    "Error 1 Cannot convert lambda expression to type 'bool' because it is not a delegate type"

    at this point:

    IQueryable

     

    <News_Item> query = from n in DataContext.News_Items where (searchQuery, searchItems);

    Maybe if you can help me sort that out... or sort anything out ... I can put this horror behind me and move on ... SOS ;)

    Thursday, February 4, 2010 6:04 PM
  • Why oh why didn't I get "PredicateBuidler" 3 days ago???

    Solved the whole problem in under an hour... they should charge 100 bucks for it...

    Thank you thank you thank you...
    • Marked as answer by PaulBinCT Thursday, February 4, 2010 9:08 PM
    Thursday, February 4, 2010 9:08 PM