馆藏中的馆藏? [英] Collections within a collection?

查看:79
本文介绍了馆藏中的馆藏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
希望有人可以指出正确的方向(或至少是关键词,这样我就可以在谷歌上搜索而不会感到困惑!).

(假设的)情况是这样的:
我有一个人对象集合,但在人对象内部是水果对象的集合.

水果对象包含有关名称,原产国和购买地点的信息.

问题1-我应该在person对象中包含水果集合,还是从数据库的角度考虑,将一个单独的水果集合链接到该人(请注意,我不能使用数据库来存储所有信息,因此是我的难题)?

问题2-主要要求之一是选择所有购买同一水果集合的人(即,该水果集合中的水果数量相同且原产地类型/国家/地区相同-在这里购买的只是一个额外的信息,但不用于匹配).关于实现这一目标的最佳方法的任何指针?

如果有人能指出我正确的方向,我将不胜感激.不用着急,我只是想知道是否可以通过一种简单的方法来实现这种假设的情况!

在此先感谢.

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.

推荐答案

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; }
}


当该树关系有意义时,集合应位于父对象中–也就是说,如果一种水果恰好与一个人相关联.对于任何单个水果项目,情况可能都是这样,但对于水果类型,事实并非如此.所以我会去找类似的东西

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;
}



然后问题2只是比较集合.将相等函数添加到FruitType以测试所需的相等"值:



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(); }
}



...,然后编写一些代码以比较您要检查的两个人中的列表. (是否有聪明的方法,或者这是排序然后比较项目"?)或者如果要进行分组,请确保仅基于集合的内容计算哈希码,然后使用Linq将对Fruits.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().


这篇关于馆藏中的馆藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆