Solved

how do i cast linc query result to ObservableCollection<>

Posted on 2010-11-10
3
1,233 Views
Last Modified: 2012-05-10
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]'.
0
Comment
Question by:Rakattack
3 Comments
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 500 total points
ID: 34109084
I think there are 2 issues with your code above
1. You can not get an ObservableCollection from a Linq iteration, for this change ObservableCollection to IEnumerable. If you really want ObservableCollection you can then convert by looping through each IEnumerable member.
2. the Select statement is wrong, you are creating an Anonymous type by saying Select new { t.ServiceBundle}
instead you should say Select new ServiceBundleLookup() { ServiceBundle =  t.ServiceBundle }
Where I am assuming the class  ServiceBundleLookup has a public field  ServiceBundle
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 34110200
Try this:

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

DaTribe
0
 

Author Closing Comment

by:Rakattack
ID: 34115856
ServiceBundles = (  from t in SBHs
                                        where t.ServiceBundle != null
                                        orderby t.ServiceBundle descending
                                        select new ServiceBundleLookup () { ServiceBundle = t.ServiceBundle }).Distinct().ToObservableCollection();
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now