?
Solved

how do i cast linc query result to ObservableCollection<>

Posted on 2010-11-10
3
Medium Priority
?
1,252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

752 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