none
Composing a List<> with LINQ RRS feed

  • Question

  • Now I apologise if this is not quite LINQ to SQL, but it's the only place I can find for this -  please redirect me if appropriate.

    I'm new to LINQ and I'd like to know if what I'm doing with this code is the right way to go. I am trying to compose a new List<> by selecting certain entries from a larger List<>. To make it more concrete, I'm writing a vocab builder for foreign language learners. The code is selecting a list of words to test from a larger selection (say, advanced vocab). It will choose some that the user got wrong last time, some that hasn't ever been tested before, and so on. So at the moment I'm trying to do it like this:

                List<Entry> testList = new List<Entry>(); // the list I am building
                List<Entry> tempList = new List<Entry>();
    
                tempList.AddRange(_wordList.Where(e => e.LastAnswerWrong == true).Take((int)(wordsPerTest/4)));
                //_wordList is another List<Entry> that is in scope
                foreach (Entry e in tempList)
                {
                    _wordList.Remove(e);
      } testList.AddRange(tempList); tempList.Clear(); tempList.AddRange(_wordList.Where(e => e.TestedCount == 0).Take((int)(wordsPerTest/4))); foreach (Entry e in tempList) { _wordList.Remove(e); } testList.AddRange(tempList); tempList.Clear(); // and so on, for each new sublist
    This seems ugly - is there a more elegant way of achieving this that doesn't involve removing the items from _wordList each time?
    class Religion { Explain (event) { try { ToExplain(event)} catch { throw("God moves in mysterious ways") } } }
    Wednesday, October 14, 2009 6:00 AM

Answers

  • Hi pedroponting,

     

    I second Syed’s suggestion.  If we understand the problem correctly, the Except LINQ extension method can be helpful on this scenario.  Besides, to make the word selection more randomly, maybe we could also use some other extension method which can pick element randomly instead of using Take. J 

     

    Here is a sample of such a random extension method, http://www.codeproject.com/KB/linq/linqrandomsample.aspx. 

     

    Some sample codes for your references:

    =======================================================================================
    var answerWrong = _wordList.Where(e => e.LastAnswerWrong == true).RandomSample((int)wordsPerTest/4, false);

    var countZero = _wordList.Except(answerWrong).Where(e => e.TestedCount == 0).RandomSample((int)wordsPerTest / 4, false);

               

    testList.AddRange(answerWrong);

    testList.AddRange(countZero);

     

    // if you also want to remove the elements in the _wordList

    _wordList = _wordList.Except(testList).ToList() ;
    =======================================================================================

     

     

    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.
    • Marked as answer by pedroponting Friday, October 16, 2009 7:18 AM
    Wednesday, October 14, 2009 8:57 AM
    Moderator

All replies

  • Hi,

    Looking at the code, it seems that you are selecting some records and then removing some based on certain criteria. In this case, Except extension method would be helpful.
    If you are still not able to figure out, then could you provide some more information providing sample data. This would help us in coming up with the desired LINQ expression for your scenario.

    Regards,
    Syed Mehroz Alam
    My Blog | My Articles
    Wednesday, October 14, 2009 7:03 AM
  • Hi pedroponting,

     

    I second Syed’s suggestion.  If we understand the problem correctly, the Except LINQ extension method can be helpful on this scenario.  Besides, to make the word selection more randomly, maybe we could also use some other extension method which can pick element randomly instead of using Take. J 

     

    Here is a sample of such a random extension method, http://www.codeproject.com/KB/linq/linqrandomsample.aspx. 

     

    Some sample codes for your references:

    =======================================================================================
    var answerWrong = _wordList.Where(e => e.LastAnswerWrong == true).RandomSample((int)wordsPerTest/4, false);

    var countZero = _wordList.Except(answerWrong).Where(e => e.TestedCount == 0).RandomSample((int)wordsPerTest / 4, false);

               

    testList.AddRange(answerWrong);

    testList.AddRange(countZero);

     

    // if you also want to remove the elements in the _wordList

    _wordList = _wordList.Except(testList).ToList() ;
    =======================================================================================

     

     

    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.
    • Marked as answer by pedroponting Friday, October 16, 2009 7:18 AM
    Wednesday, October 14, 2009 8:57 AM
    Moderator
  • Thank you both. The 'except' was exactly what I was after. I like the RandomSample thingy too!
    class Religion { Explain (event) { try { ToExplain(event)} catch { throw("God moves in mysterious ways") } } }
    Friday, October 16, 2009 7:18 AM