In my .net 3.5 win-form app, i am binding a combo box with these statements.
using (var db = new NewspaperDataContext())
{
var list = from p in db.Customers
orderby p.Name ascending
select new
{
p.Id,
p.Name
};
cboCustReport.DataSource= list;
cboCustReport.DisplayMember = "Name";
cboCustReport.ValueMember = "Id";
cboCustReport.SelectedIndex = -1;
}
But while retrieving the selected text from the combo box, i am ""(empty), If I use SelectedItem property, then I have {Id= 3 , Name = Amit Ranjan }. When I added a watch and tried to build the expression, It gave me something like this:
((<>f__AnonymousType2<int,s开发者_开发技巧tring>)(cboCustReport.SelectedItem)).Name;
Please help me, what should I use to get the value of property name.
Try to eagerly execute the query by calling .ToList()
at the end:
var list = (from p in db.Customers
orderby p.Name ascending
select new
{
p.Id,
p.Name
}).ToList();
精彩评论