Share via


How to get unique values of a field in IList

Question

Thursday, January 8, 2009 9:48 PM

 Create Table Car

(

ID int,

Model varchar;

Make nvarchar

)

I have a object car, which gets list of information from the car table

 

List<Cars> :List<Car>

 What should I do to get all unique models of Car Object.

 

Thanks

 

 

 

All replies (5)

Friday, January 9, 2009 1:11 AM âś…Answered

 I think you'll find that executing a loop on about 20,000 items would probably be pretty quick. I can't say without breaking open reflector, but I'm pretty sure that the disctinct query operator will essentially do the very same thing.

 What it sounds like is that you should possibly have another table in SQL that contains the various types of cars that are in your car table. This way when you persist your cars to the database, the CarModels table would contain very few (or certainly less than 20000 [;)] rows) models to filter on. 

 However, I would give the looping a try and see what kind of performance it gives. You might find it acceptable.

 

Edit:

  Looking at distinctiterator in Reflector, it looks like it essentially calls the Enumerator for Collection1 and then calls compare against another in memory collection. It does this as an extension method so it might save a tad on object instantiation, but we're talking milliseconds.


Thursday, January 8, 2009 10:32 PM

using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    ObjectQuery<Contact> contacts = AWEntities.Contact;
    IQueryable<string> contactsQuery = from c in contacts
                        select c.LastName;

    IQueryable<string> distinctNames = contactsQuery.Distinct();

    foreach (string name in distinctNames)
    {
        Console.WriteLine("Name: {0}", name);
    }
}

 

example

The Distinct operator is used to eliminate duplicate elements from a sequence.

var categoryNames = (
from p in products
select p.Category)
.Distinct();

this linq to entity try this

 

thank you... 


Friday, January 9, 2009 12:45 AM

Something I whipped up real fast. There are two different ways to loop through a list and find unique values. The bottom line is you'll have to loop through each item in your first list and compare the value you are checking against a second list. If the second list does not contain that value, then add it.

 

        static void ParseUniqueCars()
        {
            List<car><Car> cars = new List<car><Car>();
            Car myCar = new Car() { ID = 0, Make = "Chevy", Model = "Nogo" };
            Car myCar1 = new Car() { ID = 1, Make = "Ford", Model = "Found Dead" };

            cars.Add(myCar);
            cars.Add(myCar1);

            // list of cars now has two unique id cars.

            // get a list containing the unique models
            // the easiest way

            List<string> carModels = new List<string>();

            // loop through our list
            foreach (Car c in cars)
            {
                // if our list of car models doesn't have this model in it, add it.
                if (!carModels.Contains(c.Model))
                {
                    carModels.Add(c.Model);
                }
            }

            // a different way, not compiled, so not 100% sure on the predicate below but it should work.
            cars.ForEach(c=> 
            {
                if (!carModels.Exists(o=> o == c.Model))
                {
                    carModels.Add(c.Model);
                }
            });
        }

        class Car
        {
            public int ID { get; set; }

            public string Model { get; set; }

            public string Make { get; set; }
        }</car></car>

  


Friday, January 9, 2009 1:05 AM

Something I whipped up real fast. There are two different ways to loop through a list and find unique values. The bottom line is you'll have to loop through each item in your first list and compare the value you are checking against a second list. If the second list does not contain that value, then add it.

 

        static void ParseUniqueCars()
        {
            List<Car> cars = new List<Car>();
            Car myCar = new Car() { ID = 0, Make = "Chevy", Model = "Nogo" };
            Car myCar1 = new Car() { ID = 1, Make = "Ford", Model = "Found Dead" };

            cars.Add(myCar);
            cars.Add(myCar1);

            // list of cars now has two unique id cars.

            // get a list containing the unique models
            // the easiest way

            List<string> carModels = new List<string>();

            // loop through our list
            foreach (Car c in cars)
            {
                // if our list of car models doesn't have this model in it, add it.
                if (!carModels.Contains(c.Model))
                {
                    carModels.Add(c.Model);
                }
            }

            // a different way, not compiled, so not 100% sure on the predicate below but it should work.
            cars.ForEach(c=> 
            {
                if (!carModels.Exists(o=> o == c.Model))
                {
                    carModels.Add(c.Model);
                }
            });
        }

        class Car
        {
            public int ID { get; set; }

            public string Model { get; set; }

            public string Make { get; set; }
        }

  

 

 

Thanks for the quick reply,  I have collection of objects   like 20,000. I need only distinct models but if i try to loop around this collection and add the distinct values to another collection . Do you think it will take so much time. Is there anything like Distinct function as mentioned above  so that, it gives the result very quick. When I tried the Distinct() function it gave me the error List<string> does not contain a definition for Distinct.

Other than looping 20,000 rows Is there any other way to quickly find the distinct values. I need them because I need to filter another collection depending on these values.


Friday, January 9, 2009 1:19 AM

Can you show us the code you use to get the information from the car table?  If you are using sql you should be able to use a query something like

select distinct model from Cars;

hope that helps