Solved

how do i cast linc query result to ObservableCollection<>

Posted on 2010-11-10
3
1,236 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

837 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