In my company we have total 41 products ( U basins, T seats, W basins, Urinals, White W basins, Pink W basins ...), but the numbers of distributors are changing monthly.
We have to develop a report where we have to show the quantity of Products distributed by each distributors. So the format will be that the Product Name will form the column heading and the Distributors name will be row heading. We already know the Product Name so we can hard code in the code. But the name of Distributors is changing, so we can't hard code them.
Below is the model class and sample data set.
public class DistributionScale
{
public int DistributionScaleId { get; set; }
[Required]
public decimal Quantity { get; set; }
[ForeignKey("ProductId")]
public Product Products { get; set; }
public int ProductId { get; set; }
[ForeignKey("DistributorsId")]
public Distributors Distributors { get; set; }
public int DistributorsId { get; set; }
}
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
}
public class Distributors
{
public int DistributorsId { get; set; }
public string DistributorsName { get; set; }
}
I have provided some sample data:
List<Distributors> Distributors = new List<Distributors>(){
new Distributors{DistributorsId = 1, DistributorsName = "S S SUPPLIERS"},
new Distributors{DistributorsId = 2, DistributorsName = "HIND SUPPLIERS"},
new Distributors{DistributorsId = 3, DistributorsName = "NEXT SUPPLIERS"}
};
List<Product> Product = new List<Product>{
new Product{ ProductId = 1, ProductName = "U basins" },
new Product{ ProductId = 2, ProductName = "Urinals" },
new Product{ ProductId = 3, ProductName = "White W basins"}
};
List<DistributionScale> DistributionScale = new List<DistributionScale>(){
new DistributionScale{DistributionScaleId = 1, Quantity = 1000 ,ProductId =1 ,DistributorsId =1 },
new DistributionScale{DistributionScaleId = 2, Quantity = 1500,ProductId =2 ,DistributorsId = 2 },
new DistributionScale{DistributionScaleId = 3, Quantity = 2500,ProductId =3 ,DistributorsId = 3 }
new DistributionScale{DistributionScaleId = 4, Quantity = 1200,ProductId =1 ,DistributorsId = 3 },
new DistributionScale{DistributionScaleId = 5, Quantity = 1300,ProductId =2 ,DistributorsId = 1 }
};
Below is the simple query what I am trying to do. I do not how to make the product name as column heading and show the distribution data as attached in the image.
public JsonResult ReportDistributionScale()
{
var res = (from n in db.DistributionScale
join s in db.Product on n.ProductId equals s.ProductId
join k in db.Distributors on n.DistributorsId equals k.DistributorsId
orderby n.ProductId
select new DistributionScaleViewModel
{
ProductName = s.ProductName
,DistributorsName = k.DistributorsName
,Quantity = n.Quantity
}).ToList();
return Json(res, JsonRequestBehavior.AllowGet);
}
//// another way I am trying
var res = from n in db.DistributionScale
join s in db.Product on n.ProductId equals s.ProductId
join k in db.Distributors on n.DistributorsId equals k.DistributorsId
group new
{
s.ProductName
,k.DistributorsName
,n.Quantity
} by ProductName into g
select new {
///// how to proceed further
}).ToList();
Attached is the image. How can I write LINQ to get the final output as shown in the image.