This is a migrated thread and some comments may be shown as answers.

Linq and Dropdown lists

3 Answers 96 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
jon
Top achievements
Rank 1
jon asked on 27 May 2009, 11:23 PM
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)
{
}

3 Answers, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 28 May 2009, 07:44 AM
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.
0
jon
Top achievements
Rank 1
answered on 28 May 2009, 02:06 PM
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.
0
Accepted
PetarP
Telerik team
answered on 28 May 2009, 03:42 PM
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.
Tags
Getting Started
Asked by
jon
Top achievements
Rank 1
Answers by
PetarP
Telerik team
jon
Top achievements
Rank 1
Share this question
or