Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I hit a stumbling block and cannot break through.  When I add a product it can be at both branches, if for example theres 2. Now the problem arises when I want to display a product on the store front.  How do I display just one of the duplicate product but the quantity to be combined from both branches and displayed  


What I have tried:

I have a table


public class Branch
{
public int Id{get;set;}
public string BranchName{get;set;}
}









public class product
{
public int Id {get;set;}
public string Name{get;set;}
public int qty {get;set}






[ForeignKey("Branch")]
public int BranchId {get;set;}
public string branchname{get;set;}
public Branch Branch {get;set;}
}
Posted
Updated 18-Jul-18 14:13pm
v2
Comments
Richard Deeming 19-Jul-18 10:50am    
That looks like a bad design. There doesn't seem to be any obvious way to connect a product at one branch to the same product at a different branch. Both records would have different IDs.

I'd suggest having three entities:
Branch: (Id, BranchName)
Product: (Id, Name)
BranchProducts: (BranchId, ProductId, Quantity)

That gives you a "many-to-many" connection between branches and products, with an extra "quantity" attribute on the connecting table.

1 solution

Query "product" by "Id" (or name, if unique) and "Sum" "qty":

Enumerable.Sum Method (System.Linq) | Microsoft Docs[^]
 
Share this answer
 

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