Link to home
Start Free TrialLog in
Avatar of Rakattack
Rakattack

asked on

how do i cast linc query result to ObservableCollection<>

i have the following ob collection defined :
public ObservableCollection<ServiceBundleLookup> ServiceBundles{



iwant to set it to the result of this link query :
ServiceBundles = (ObservableCollection<ServiceBundleLookup>)(from t in SBHs
                                                                                where t.ServiceBundle != null
                                                                                orderby t.ServiceBundle descending
                                                                                select new { t.ServiceBundle }).Distinct();

But am getting trhe following error :

Unable to cast object of type '<DistinctIterator>d__81`1[<>f__AnonymousType0`1[System.String]]' to type 'System.Collections.ObjectModel.ObservableCollection`1[Bnz.TTSC.Client.Entities.ServiceBundleLookup]'.
ASKER CERTIFIED SOLUTION
Avatar of arif_eqbal
arif_eqbal

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Avodah
Try this:

ServiceBundles = new ObservableCollection<ServiceBundleLoopup>
(
   SBHs
      .Where(t => t.ServiceBundle != null)
      .OrderByDescending(t => t.ServiceBundle)
      .Select(t => t.ServiceBundle)
      .Distinct()
);

DaTribe
Avatar of Rakattack
Rakattack

ASKER

ServiceBundles = (  from t in SBHs
                                        where t.ServiceBundle != null
                                        orderby t.ServiceBundle descending
                                        select new ServiceBundleLookup () { ServiceBundle = t.ServiceBundle }).Distinct().ToObservableCollection();