none
AsEnumerable not working with select RRS feed

  • Question

  • Regex rx WordCounter(@"\b(\w[-']+\b);
    
    var query = from article in  DataContext.MedicalArticles
    where(article => article.Topic =="Influenza")
    .AsEnumerable()
    where (article => rx.Matches(article.Abstract).Count < 100) 
    select article);


    This is not working, although I am basically following an example in the Linq Pocket Reference page 40.
    • Edited by Mattaniah Friday, September 18, 2009 10:59 AM
    Friday, September 18, 2009 8:33 AM

Answers

  • Hi Mattaniah,

    Yes, you can definitely use query comprehension syntax.  Based on the email example in my last post, we can use the LINQ operator instead of the LINQ extension methods as the following:
    ==================================================================
    Regex rx = new Regex(@"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", RegexOptions.IgnoreCase);

    var query = from u in

                    (from u in db.Users

                     where u.UserID > 2

                     select u).AsEnumerable()

                where rx.Match(u.Email).Success

                select u.Email;
    ==================================================================
    The query will return the all the standard email address of the Users whose UserID is greater than 2.  

    Do you encounter any problems from your query or incorrect result?   Would you mind providing us with more detailed information? 


    Have a nice weekend!

     

    Best Regards,
    Lingzhi Sun


    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.
    • Marked as answer by Mattaniah Friday, September 18, 2009 1:54 PM
    Friday, September 18, 2009 1:44 PM
    Moderator

All replies

  • Hi Mattaniah,

     

    Your codes should be the same as the sample codes in “LINQ Pocket Reference”:

    ==============================================================
                Regex wordCounter= new Regex (@"\b(\w|[-'])+\b");

     

                var query = dataContext.MedicalArticles

                  .Where (article => article.Topic == "influenza")

                  .AsEnumerable()

                  .Where (article =>

                    wordCounter.Matches (article.Abstract).Count <100);
    ==============================================================

     

    Do you encounter any problems or retrieve the incorrect result?  Please use some codes like DataContext.Log = Console.Out; to debug the SQL command generated by this LINQ query.  The SQL command should be only contains the first Where filter.  After calling AsEnumerable(), the data has been retrieved into local memory, so the second Where filter actually works as LINQ to Objects does. 

     

    The problem could also be caused by the regular expression part.   The following regular expression is to check email address.  You can use it to test some data column which saves email addresses.  The testing result is correct at my side.

    ==============================================================
    Regex rx = new Regex(@"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", RegexOptions.IgnoreCase);

    var query = db.Users.Where(u => u.UserID > 2).AsEnumerable().Where(u => rx.Match(u.Email).Success);

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

    Besides, here is one forum thread where Joe has provided some suggestions on LINQ to SQL and Regular Expressions for your references:
    http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/59a36b8a-a5a6-45a9-9c02-7fc3b218085d


    Hope you have a nice day!

     

     

    Best Regards,
    Lingzhi Sun


    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.
    Friday, September 18, 2009 10:22 AM
    Moderator
  • Why can't I use query comprehension syntax (from t in dataContaxt.Table where ... select)


    The goal is not to retrieve all records but only those records I need to check with the Regular Expression.
    Friday, September 18, 2009 10:58 AM
  • Hi Mattaniah,

    Yes, you can definitely use query comprehension syntax.  Based on the email example in my last post, we can use the LINQ operator instead of the LINQ extension methods as the following:
    ==================================================================
    Regex rx = new Regex(@"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", RegexOptions.IgnoreCase);

    var query = from u in

                    (from u in db.Users

                     where u.UserID > 2

                     select u).AsEnumerable()

                where rx.Match(u.Email).Success

                select u.Email;
    ==================================================================
    The query will return the all the standard email address of the Users whose UserID is greater than 2.  

    Do you encounter any problems from your query or incorrect result?   Would you mind providing us with more detailed information? 


    Have a nice weekend!

     

    Best Regards,
    Lingzhi Sun


    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.
    • Marked as answer by Mattaniah Friday, September 18, 2009 1:54 PM
    Friday, September 18, 2009 1:44 PM
    Moderator