Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi...
excuse me.how do i can get first and last item combobox with linq
very Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 13-Apr-11 23:47pm    
Tag it! WPF, WinForm, ASP.NET, what?
--SA
Wild-Programmer 13-Apr-11 23:47pm    
Please explain??

Why linq?

C#
String first = (From item In ComboBox1.Items).FirstOrDefault;
String last = (From item In ComboBox1.Items).LastOrDefault;


Isn't the normal way easier?!
C#
String first = ComboBox1.Items[0];
String last = ComboBox1.Items[ComboBox1.Items.Count-1];
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 14-Apr-11 11:51am    
Correct, my 5. I was good to show LINQ anyway.
--SA
Espen Harlinn 14-Apr-11 16:49pm    
Nice reply, my 5
Linq and ComboBox dont work well.

Try this approach.
ComboBox cb = new ComboBox();
List<object> list = cb.Items.Cast<object>().ToList();
object first = list.First();
object last = list.Last();


Good luck!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Apr-11 11:56am    
Items.Cast is not needed, but it was good to show this technique. My 5.
--SA
Why on Earth it should be LINQ? This is simply myComboBox.Items[0] and myComboBox.Items[myComboBox.Items.Count-1]

The only reason I can see it is somebody told you to do that, but we don't have to answer such Questions; in this case that person should ask CodeProject, not you. This is how it works.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900