locked
Linq GroupBy Error RRS feed

  • Question

  • Hello,

    I have this code block in which I am trying to bind the QuestionText and Id to a dropdown box at runtime.  I have to group by the Question Text but keep getting an error on the DataBind() -

    DataBinding: 'System.Linq.Lookup`2+Grouping[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[<>f__AnonymousType0`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], DeloitteDEXCharting, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' does not contain a property with the name 'QuestionText'.

    What am I doing wrong?

    var results = from q in db.GetFilterQuestionsAndAnswers(templateId) select new { QuestionText = q.QText, QuestionId = q.QId }; ddFilterQuestion.DataSource = results.GroupBy(r => r.QuestionText); ddFilterQuestion.DataTextField = "QuestionText"; ddFilterQuestion.DataValueField = "QuestionId"; ddFilterQuestion.DataBind();
    Friday, October 9, 2009 7:40 PM

Answers

  • I found how to do it but seems like overkill.   Thought I could use a lamda expression to do it but this did the trick.   If you can use a lamda expression I would love to know how!

                var questions = from q in db.GetFilterQuestionsAndAnswers(templateId)
                                group q by new
                                {
                                    QText = q.QText,
                                    QId = q.QId
                                }
                                    into grp
                                    select new
                                    {
                                        QuestionText = grp.Key.QText,
                                        QuestionId = grp.Key.QId
                                    };
    
    Friday, October 9, 2009 8:00 PM
  • Hi Jason,

    Here's the equivalent query using lambda expressions (I used LINQPad to convert it ):

    var questions = db.GetFilterQuestionsAndAnswers(templateId) .GroupBy ( q => new { QText = q.No, QId = q.Description } ) .Select ( grp => new { QuestionText = grp.Key.QText, QuestionId = grp.Key.QId } );

    However I would still prefer the non-lambda syntax that you posted in your second post since it is much easier to read.


    Regards,

    Syed Mehroz Alam
    My Blog | My Articles
    Saturday, October 10, 2009 2:40 PM
  • Hi Jason,

     

    After querying the database using C# statements in LINQPad, please select the “λ” (Lambda Expressions) in the result window.  We will see the corresponding lambda syntax there.

     

     

    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.
    Tuesday, October 13, 2009 2:30 AM

All replies

  • I found how to do it but seems like overkill.   Thought I could use a lamda expression to do it but this did the trick.   If you can use a lamda expression I would love to know how!

                var questions = from q in db.GetFilterQuestionsAndAnswers(templateId)
                                group q by new
                                {
                                    QText = q.QText,
                                    QId = q.QId
                                }
                                    into grp
                                    select new
                                    {
                                        QuestionText = grp.Key.QText,
                                        QuestionId = grp.Key.QId
                                    };
    
    Friday, October 9, 2009 8:00 PM
  • Hi Jason,

    Here's the equivalent query using lambda expressions (I used LINQPad to convert it ):

    var questions = db.GetFilterQuestionsAndAnswers(templateId) .GroupBy ( q => new { QText = q.No, QId = q.Description } ) .Select ( grp => new { QuestionText = grp.Key.QText, QuestionId = grp.Key.QId } );

    However I would still prefer the non-lambda syntax that you posted in your second post since it is much easier to read.


    Regards,

    Syed Mehroz Alam
    My Blog | My Articles
    Saturday, October 10, 2009 2:40 PM
  • Thanks - I am using LinqPad as well.  How did you use to convert?
    Monday, October 12, 2009 1:14 PM
  • Hi Jason,

     

    After querying the database using C# statements in LINQPad, please select the “λ” (Lambda Expressions) in the result window.  We will see the corresponding lambda syntax there.

     

     

    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.
    Tuesday, October 13, 2009 2:30 AM
  • Hi Jason,

     

    Do you need any further assistance?  If so, please feel free to let me know.

    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.
    Tuesday, October 20, 2009 12:46 AM
  • All set, thanks!
    Tuesday, October 20, 2009 1:11 PM