Solved

how do i cast linc query result to ObservableCollection<>

Posted on 2010-11-10
3
1,228 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
Comment Utility
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
Comment Utility
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
Comment Utility
ServiceBundles = (  from t in SBHs
                                        where t.ServiceBundle != null
                                        orderby t.ServiceBundle descending
                                        select new ServiceBundleLookup () { ServiceBundle = t.ServiceBundle }).Distinct().ToObservableCollection();
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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