kristofdielis
Programmer
- Sep 1, 2011
- 25
Hi,
I have two comboboxes, one containing all sites, the other contains all units. The units depends on the sites, so if a site is selected, only those units that belong to the selected site should be fetched.
First I check if there is a selected item:
So site either contains a valid SITE instance, or null, so now we can check that in the LINQ query:
Or, I thought that would work, but I get a NullValueException on the units IEnumerable, if site is null.
Any suggestions on how to improve the provided query?
Thx.
I have two comboboxes, one containing all sites, the other contains all units. The units depends on the sites, so if a site is selected, only those units that belong to the selected site should be fetched.
First I check if there is a selected item:
Code:
WPF.Model.SITE site = (cbxSites.SelectedItem != null ? (WPF.Model.SITE)cbxSites.SelectedItem : null);
So site either contains a valid SITE instance, or null, so now we can check that in the LINQ query:
Code:
var units = from u in db.UNIT
where site == null || (site != null && u.site_id == site.site_id)
orderby u.name
select u;
Or, I thought that would work, but I get a NullValueException on the units IEnumerable, if site is null.
Any suggestions on how to improve the provided query?
Thx.