Linq and Dropdown lists

4 posts, 1 answers
  1. jon
    jon avatar
    33 posts
    Member since:
    Dec 2008

    Posted 27 May 2009 Link to this post

    I am having a lot of trouble getting my "result" to transfer over into the "list" variable.  Should be simple, but i'm missing something.  The error is:  Using the generic type 'System.Collections.Generic.IList<T>' requires '1' type arguments   
    Please advise:

    var result = from s in scope.Extent<Source>()
               select new { s.IdSource, s.SourceName };

    IList list = result.ToList();

    foreach (Source data in list)
    {
    }
  2. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 28 May 2009 Link to this post

    Hello jon,
    the IList requires to have a type passed. You will need to declare it like this:
    IList<Source> result =(IList<Source>) result.ToList();
    The following code will compile but it will give a runtime error. The reason is that you cannot cast the Anonymous type to type source.
    You can use the following instead:
    IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope(); 
                var result = from c in scope.Extent<Source>() select new { c.IdSource, c.SourceName};             
                foreach (var obj in result) 
                { 
                    Console.WriteLine(obj.IdSource); 
                    Console.WriteLine(obj.SourceName); 
                } 
                scope.Dispose(); 
    If you want to bind the result to a combo box for example than you can use it directly and bind it like this:
     IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope(); 
                var result = from c in scope.Extent<Source>() select new { c.idSource, c.SourceName}; 
                RadComboBox1.DataSource = result; 
                RadComboBox1.DataBind(); 


    All the best,
    PetarP
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft banner
  4. jon
    jon avatar
    33 posts
    Member since:
    Dec 2008

    Posted 28 May 2009 Link to this post

    Binding directly to the combo box returned this result:
    <select name="ctl00$maincontent$ddlSource" id="ctl00_maincontent_ddlSource">
     <option value="{ IdSource = 1, SourceName = BigAls }">{ IdSource = 1, SourceName = BigAls }</option>
     <option value="{ IdSource = 3, SourceName = Happys Lab }">{ IdSource = 3, SourceName = Happys Lab }</option>

    How can I fix that?  Thank you.
  5. Answer
    PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 28 May 2009 Link to this post

    Hello jon,
    this is the result of your query. What would you like to fix ? When you bind it to a combo box than the combo box will display the selected fields and the corresponding values to them in such brackets. If you would like to display only one of them then you should take advantage of the DataTextField. This property specifies which field to be shown in the drop down list.

    Sincerely yours,
    PetarP
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top