Get uniqe property list from ObservableCollection using Linq

I have a object model that has ID, Name and Category properties

I have another object model that is an ObservableCollection of my first model

I want to get all the unique categories from my collection using Linq, but I am not sure how to do it...

I was using something like:

            var sel = (from i in items
                      select new
                      {
                          i.Category
                      }).Distinct();

but I am getting a null value from it (there are 1000 test records in the collection all with categories)


New to Linq, so I am not sure what I am doing wrong.
S7up1dAsked:
Who is Participating?
 
S7up1dConnect With a Mentor Author Commented:
I did not ever get an answer for this question that was even using the correct framework.
0
 
Meir RivkinFull stack Software EngineerCommented:
what is the type of Category?
0
 
S7up1dAuthor Commented:
Sorry, should have included that....

Category is string
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Meir RivkinFull stack Software EngineerCommented:
the linq expression is correct.
if u remove the Disticnt() call, do u get all categories?
0
 
S7up1dAuthor Commented:
I get nothing...  I get this error:

System.Collections.Generic.IEnumerator<TSource>.Current = Could not evaluate expression
0
 
Meir RivkinFull stack Software EngineerCommented:
ok, so we need to go one step back and examine the items collection.
if you debug the code and "Watch" the items collection, can u view its items?
how do u fill the items collection?
0
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi S7up1d;

Code like this works.

ObservableCollection<MyObjectModel> MyObservable = new ObservableCollection<MyObjectModel>( );
// Initialize the ObservableCollection MyObservable here
// ...

// Linq query using MyObservable collection of MyObjectModel
var sel = (from i in MyObservable
           select new 
           {
               i.Category
           }).Distinct();

// Iterate through the collection
foreach( var s in sel )
{
    Console.WriteLine( s.Category );
}

// ...

public class MyObjectModel
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
}

Open in new window


Note in my code I am using the ObservableCollection of type MyObjectModel in the query. Is your code is items variable of  ObservableCollection of type MyObjectModel? If it is can you please post your code as I did so that we can see the whole picture.

Thank
Fernando
0
 
S7up1dAuthor Commented:
so, the code I had was working, I was misinterpreting the errors and not iterating through the collection to check it.  A bit hairbrained on my part.

The last part of this is that I cannot get it to return the observable collection of properties as an observable collection... I get invalid cast exception when I use:


                    categories = (ObservableCollection<string>)(  from i in items
                                                                  select new
                                                                  {
                                                                    i.Category
                                                                  }).Distinct();


0
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi S7up1d;

There is no direct conversion to an ObservableCollection from Linq. So this is what you need to do:

IEnumerable<string> sel = 
    ( from i in MyObservable
      select 
          i.Category
    ).Distinct( );

ObservableCollection<string> categories = new ObservableCollection<string>(sel);

Open in new window


That should give you an ObservableCollection of Strings.

Fernando
0
 
S7up1dConnect With a Mentor Author Commented:
I tried this:

                   IEnumerable<string> distinctCategories = (from i in items
                                                                  select
                                                                    i.Category
                                                            ).Distinct();

                    categories = new ObservableCollection<string>(distinctCategories);  



I get the IEnumerable<string> but when I try to assign the categories to a new ObservableCollection<string> it will not go as there is no constuctor that accepts a single argument.  

Is there another way to convert this or do I have to just use the IEnumerable?
0
 
Fernando SotoRetiredCommented:
What data type is categories?
0
 
Fernando SotoRetiredCommented:
BTW I just test the code I posted to you and it does work on my system.
0
 
S7up1dAuthor Commented:
ObservableCollection<string> categories;

0
 
Fernando SotoRetiredCommented:
Hi S7up1d;

Can you please post all the pertinent code copied from your project and past here so I can have a look. As I stated I have tested this code and it does work.

The three overloaded constructors for the ObservableCollection<T> from Microsoft Documentation:
ObservableCollection<T>()
ObservableCollection<T>(IEnumerable<T>)
ObservableCollection<T>(List<T>)               // The one used in the example above

Open in new window


Fernando
0
 
Fernando SotoRetiredCommented:
What version of .Net and C# are you using?
0
 
Fernando SotoRetiredCommented:
Hi S7up1d;

It appears that in .Net version 3.5 the ObservableCollection only had the default constructor and the reason for the error you are getting. Try this version of the code to get what you need.

IEnumerable<string> sel = 
    ( from i in MyObservable
      select 
          i.Category
    ).Distinct( );

ObservableCollection<string> categories = new ObservableCollection<string>();

foreach( string c in sel )
{
    categories.Add( c );
}

Open in new window


Fernando
0
 
Fernando SotoRetiredCommented:
Has my last post corrected the issue?
0
 
kumar754Commented:
well, I thought .NET 3.5 also has the same 3 constructors asin .NET 4.0

Reference:
http://msdn.microsoft.com/en-us/library/ms658737(v=VS.90).aspx

check your using/imports namespace

using System.Collections.ObjectModel;


Also, if you are using .net 3.0, then try convert it to List instead of IEnumerable and then pass to Observable Collection


List<string> sel =
    ( from i in MyObservable
      select
          i.Category
    ).Distinct();

ObservableCollection<string> categories = new ObservableCollection<string>(sel);
0
 
S7up1dAuthor Commented:
I am using .NET 4.0 CF for Windows Phone.  

While I can iterate through the list or the ienumerable and then copy into the ObservableCollection, I would rather not, I would imagine that there is a better way to do that than having two copies of the list in memory during the transfer process.

0
 
Fernando SotoRetiredCommented:
@ kumar754;

Following your link shows the three constructors. Investigating a little bit more by clicking on "ObservableCollection<T> Class" just below the three constructors definition you will find the Change History as shown in the image below. So in version 3.5 just default constructor and in version 3.5 SP1 added new constructors.

Fernando Change History
0
 
kumar754Commented:
@FernandoSoto

Thanks buddy, for the sharing this information. Yeah you are right, they added in 2008, good to know that I am using the same SP1 version.
0
 
Fernando SotoRetiredCommented:
Hi S7up1d;

If you can upload the project and all its files I will attempt to see what is happening. If you can follow the instructions below.

To upload a file in zip format to the Experts Exchange Stuff web site at http://www.ee-stuff.com follow these steps:

1.  Zip the files or project to be uploaded
2.  The go to http://www.ee-stuff.com
3.  If you are prompted to log in use the same username and password you use on the main site here
4.  At the top of the page click on "Expert Area" tab
5.  Then click on "Upload a new file" link
6.  Follow the instructions on the page
7.  After a successful upload post the link of the file in the question.

Fernando
0
 
S7up1dAuthor Commented:
I am using .NET 4.0 For WINDOWS PHONE

not 3.5...

I will have to rewrite some of the code into an example I can post here.
0
 
Fernando SotoRetiredCommented:
You should of continued to work with us and follow through with your response from post 03/06/11 02:18 PM, ID: 35047200.
0
 
S7up1dAuthor Commented:
Not on the correct framework at all, but I do want to give the points for the work done.

Thanks for your help!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.