Reputation: 3575
I am trying to populate combobox items
with list of objects
that I retrieve from linq to sql
in following way, (which only works for me with select new
).
Note that the Tax
parameters are exactly the same as the select new.
dt = new LilkaDataContext();
var taxes = from p in dt.Taxes select new
{
Id = p.Id,
Name = p.Name,
Percentage = p.Percentage,
Note = p.Note
};
cb.ItemsSource = taxes;
XAML
<ComboBox Name="CbSettingsTax" Grid.Column="2"
SelectedValue="Id"
DisplayMemberPath="Name" >
</ComboBox>
Now I need to parse the value and retrieve the Tax Id that is selected in combobox that I populated.
int selectedTax = ((Tax)CbProductTax.SelectedItem).Id;
And got following error because of select new
creating new Object. I wonder if there is any way either to parse the last step to get Tax.Id
or to better populate without select new
/creating new object.
Additional information: Unable to cast object of type '<>f__AnonymousType1
4[System.Int32,System.String,System.Nullable
1[System.Decimal],System.String]' to type 'lilka.Tax'.
Edit:
dt = new LilkaDataContext();
var taxes = from p in dt.Taxes select p;
cb.ItemsSource = taxes;
Using this query I am unable to select item from Combobox Item list in GUI. It just does not appear as Selected Item.
Upvotes: 0
Views: 4620
Reputation: 5666
First of all let's talk about your ComboBox: you should use SelectedValuePath
property instead of SelectedValue
. So:
<ComboBox Name="CbSettingsTax" Grid.Column="2"
SelectedValuePath="Id"
DisplayMemberPath="Name" />
In this way in your code CbSettingsTax.SelectedValue
will return the Id value of the selected Tax object, while CbSettingsTax.SelectedItem
will return the whole selected object.
About Linq-To-Sql, I see you are using "Query Syntax". Personally I prefer "Method Syntax". When you write
var taxes = from p in dt.Taxes select new
{
Id = p.Id,
Name = p.Name,
Percentage = p.Percentage,
Note = p.Note
};
since you are using the new keyword, the compiler creates an Anonimous Type for you, which has 4 properties called Id, Name, Percentage and Note. Then LINQ copies Tax property values in the new class ones.
However this anonimous type is not the same as Tax
type. Therefore you cannot cast it to Tax
. Indeed taxes is a collection of this anonymous type. It is not a collection of Tax objects.
Previously I told you I prefer LINQ "Method Syntax", since every LINQ method returns a generic IEnumerable. Of course you can obtain the same result with query syntax, but it is easy to mistake.
If you use (for example):
dt = new LilkaDataContext();
cb.ItemsSource = dt.Taxes.Where(tax => tax.Name.Contains("VAT"));
your combobox will have a collection of Tax objects as ItemsSource.
Now you just need to choose your approach. You can use "Method Syntax" and then remove the SelectedValuePath
property from your combobox. On the other side you can continue using "Query Syntax", but in this case you have to use SelectedValuePath
property and your code will be:
int id = (int)CbProductTax.SelectedValue;
I hope it can help you.
Upvotes: 1
Reputation: 5825
Just put the data directly from the query instead of creating a new object:
dt = new LilkaDataContext();
var taxes = from p in dt.Taxes select p;
cb.ItemsSource = taxes.ToList();
Upvotes: 2