Reputation:
for a project i'm currently working on, im trying to pass a set of objects all somehow linked to one object.
I'm currently blacked out and can't seem to find a proper solution.
The situation is like this. I have a product object, on which a few insurance packages apply. I need to pass this information to the view, but I have to be able to retrieve the proper packages for the proper products. so it looks like this...
Product 1 has package 1, 2, 3 Product 2 has package 2,3,5,6 Product 3 has package 2,4,6,7
The problem is that there can be a different number of products and a different number of packages
Any ideas? The answer is probably simple, but I'm a little bit too tired to find it out...
Upvotes: 0
Views: 1654
Reputation: 34396
How about this:
The GetPackages method can query the internal dictionary to determine which packages to return.
public class ProductsPackages
{
private Dictionary<int, int> _map;
public ProductsPackages(List<Product> products, List<Package> packages,
Dictionary<int, int> map)
{
_map = map;
}
public List<Product> Products { get; private set; }
public List<Package> Packages { get; private set; }
public List<Package> GetPackages(Product product)
{
return (from p in Packages
join kvp in _map on p.ID == kvp.Value
where kvp.Key == product.ID
select p).ToList();
}
}
Upvotes: 0
Reputation: 19627
Perhaps I misunderstand the question, but if it's simply a matter of passing these data to the view, the most straightforward might be to create a method which takes a product as one parameter and a collection of packages as another parameter, something like:
public void SetProduct(Product product, IList<Package> packages)
{
...
}
Upvotes: 0
Reputation: 11528
A dictionary of lists?
Dictionary<Product, List<Package>> products = new Dictionary<Product, List<Package>>();
products.Add(product1, new List<Package>());
products.Add(product2, new List<Package>());
products.Add(product3, new List<Package>());
products[product1].Add(package1);
products[product1].Add(package2);
products[product2].Add(package2);
products[product2].Add(package3);
products[product2].Add(package5);
products[product2].Add(package6);
products[product3].Add(package2);
products[product3].Add(package4);
products[product3].Add(package6);
products[product3].Add(package7);
I do this often enough I wrote my own container IndexedLists<K,V>
which is really a Dictionary<K, List<V>>
that automatically handles creation of the lists.
Upvotes: 5
Reputation: 297
I think we need more information... Are your product and package objects linked in any way? If the product object has a list of packages that apply then you simply pass the product to the view and move on. If not then how are you keeping track of the linkage between your two object types?
Upvotes: 0
Reputation: 27509
From the sound of it you need to wrap the objects up in some container object. The container object constructor would take all the objects you want to group together, then you can pass the container object.
If your passing things up to a view via an event, perhaps you could use a custom class that inherits from the EventArgs class.
Upvotes: 0