Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi All,
Hoping someone can point me in the right direction (or at least the key words so I can google it without getting bamboozled with it!).

The (hypothetical) scenario is this:
I have a collection of person objects but within the person object is a collection of fruit objects.

The fruit object contains information on name, country of origin and where bought.

Question 1 - should I have the collection of fruits within the person object, or, thinking about it from a database point of view, have a separate collection of fruits linked up to the person (note, I can't use a database to store all the information, hence my conundrum)?

Question 2 - One of the main requirements will be to select all people who buy the same collection of fruits (i.e. the number of fruits in the collection is the same and the type/country of origin is this same - where bought is just an additional piece of info but not used for matching). Any pointers on the best way around to achieve this?

If someone could please point me in the right direction, I'd appreciate it. No hurry as I'm just wondering if this hypothetical scenario is achievable in an easy way!

Thanks in Advance.
Posted
Updated 13-Jun-11 0:24am
v2
Comments
Nebula2028 14-Jun-11 5:40am    
Thanks for the idea's and help all. I can see ideas from both solutions which I can use but Bob's solution does feel closer to what I'm after. Next steps for me will be digging deeper into LINQ queries I think! Thanks again.

class Person
{
    public string Name { get; set; }
    public Fruits FruitsItem { get; set; }
    //now you can get the details like this....
    List<Person> _allPerson = new List<Person>();
    private List<Person> CreateCollection()
    {
        _allPerson.Add(new Person { Name = "person1", FruitsItem = new Fruits { Name = "Mango", Country="India", Origin="origin", PurchaseLocation="Banglure" } });
        _allPerson.Add(new Person { Name = "person2", FruitsItem = new Fruits { Name = "Orange", Country = "India", Origin = "origin", PurchaseLocation = "Banglure" } });
        _allPerson.Add(new Person { Name = "person3", FruitsItem = new Fruits { Name = "Mango", Country = "India", Origin = "origin", PurchaseLocation = "Banglure" } });
        _allPerson.Add(new Person { Name = "person4", FruitsItem = new Fruits { Name = "Apple", Country = "India", Origin = "origin", PurchaseLocation = "Banglure" } });
        _allPerson.Add(new Person { Name = "person5", FruitsItem = new Fruits { Name = "Mango", Country = "India", Origin = "origin", PurchaseLocation = "Banglure" } });
        return _allPerson;
    }
    private List<Person> GetCollection()
    {
        var temp = CreateCollection().ToArray();
        var item = from t in temp
                    where t.FruitsItem.Name = "Apple" //your choicc
                    select t;
        return item;
    }
}
class Fruits
{
    public string Name { get; set; }
    public string Origin { get; set; }
    public string Country { get; set; }
    public string PurchaseLocation { get; set; }
}
 
Share this answer
 
Comments
BobJanova 13-Jun-11 9:22am    
This solution will give people 1 and 5 a different instance of 'Mango/India'. In database terms, it is not normalised.
The collection should be within the parent object when that tree relationship makes sense – i.e., if a fruit is associated with exactly one person. For any individual item of fruit that may be the case, but for fruit types, it is not. So I would go for something like

class Person {
 List<FruitItem> Fruits;

 string Name, Address; // etc
}

class FruitItem {
 FruitType Type;
 int Mass;
 Location WhereBought;
 // Anything else that applies to THIS PARTICULAR piece of fruit
 // e.g. condition, ID number
}

class FruitType {
 string Name;
 string CountryOfOrigin;
 // Anything else that applies to THIS TYPE of fruit
 // e.g. supplier, class, maybe batch number
}

class DataModel {
 List<Person> People;
 List<FruitType> FruitTypes;
}


Question 2 is then just comparing collections. Add equality functions to FruitType to test what you want as 'equal':
class FruitItem {
 ...

 public override Equals(object other){
  FruitItem otherFruit = other as FruitItem;
  return otherFruit != null && otherFruit.FruitType == FruitType;
 }

 public override int GetHashCode() { return FruitType.GetHashCode(); }
}


... and then write some code to compare the lists in the two people you want to check. (Is there a clever way, or is this 'sort and then compare items'?) Or if you want to do a grouping, make sure that the hash codes for a collection are calculated based on only their contents, and then use Linq to group on Fruits.GetHashCode().
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900