# ICollection in different model

• ### Question

• User733430484 posted

usually we can put all model in one ICollection.

In case I have two model. One in Model Cal_2 and another in Model Cal_3. What I want try to is;

put the all model Cal_1 into Cal_2 then put Cal_2 into Cal_3.I need the result can show like this;

 A B NCount Rb AnB A^2 B^2 10.1 20.2 1 10.1 204.2 102.01 408.04 20.1 40.4 2 40.2 812.04 404.01 1632.16 30.3 60.6 3 90.9 1836.18 918.09 3672.36 40.4 79.9 4 161.6 3227.96 1632.16 6384.01 100.9 201.1 4 302.8 6080.2 3056.27 120.96.57 Sigma_AB Sigma_AxB Sigma_Apower Sigma_A 24320.8 20290.99 12225.08 10180.81 Value of b 1.971270918

Here is the code.

1.Controller

```using Calculate_5.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Calculate_5.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
List<AB> FillData = new List<AB>  {
new AB{A=10.1m,B=20.2m},
new AB{A=20.1m,B=40.4m},
new AB{A=30.3m,B=60.6m},
new AB{A=40.4m,B=79.9m}
};

var Result = new List<Cal_1>();

int V = 0;
for (int i = 0; i < FillData.Count; i++)
{
V++;
Result.Add(new Cal_1 { A = FillData[i].A, B = FillData[i].B, NCount = V, Ra = FillData[i].A * V, Rb = FillData[i].B * V, APower = (decimal)Math.Pow((double)FillData[i].A, 2), BPower = (decimal)Math.Pow((double)FillData[i].B, 2), AnB = FillData[i].A * FillData[i].B });
}

decimal a = FillData.Sum(m => m.A);
decimal b = FillData.Sum(m => m.B);
decimal ra = Result.Sum(m => m.Ra);
decimal rb = Result.Sum(m => m.Rb);
decimal anb = Result.Sum(m => m.AnB);
decimal apower = Result.Sum(m => m.APower);
decimal bpower = Result.Sum(m => m.BPower);

{
A = a,
B = b,
NCount = V,
Ra = ra,
Rb = rb,
AnB = anb,
APower = apower,
BPower = bpower,

});
var vm = new Cal_2
{
Sigma_AB = V * anb,
Sigma_AxB = a * b,
Sigma_Apower = V * apower,
Sigma_A = (decimal)Math.Pow((double)a, 2),
Cal_1s = Result
};

var vm_Equation = new List<Cal_3>();
{
vm_Equation.Add(new Cal_3 { Find_b = (vm.Sigma_AB - vm.Sigma_AxB) / (vm.Sigma_Apower - vm.Sigma_A) });
};

var vm_Final = new Cal_3
{
Find_b = (vm.Sigma_AB - vm.Sigma_AxB) / (vm.Sigma_Apower - vm.Sigma_A),
Cal_2s = vm

};

return View(vm_Final);
}
}
}
```

2.Model

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Calculate_5.Models
{
public class AB
{
public decimal A { get; set; }
public decimal B { get; set; }
}
public class Cal_1
{
public decimal A { get; set; }
public decimal B { get; set; }
public int NCount { get; set; }
public decimal Ra { get; set; }
public decimal Rb { get; set; }
public decimal APower { get; set; }
public decimal BPower { get; set; }
public decimal AnB { get; set; }
}
public class Cal_2
{
public decimal Sigma_AB { get; set; }
public decimal Sigma_AxB { get; set; }
public decimal Sigma_Apower { get; set; }
public decimal Sigma_A { get; set; }
public ICollection<Cal_1> Cal_1s { get; set; }
}
public class Cal_3
{
public decimal Find_b { get; set; }
public ICollection<Cal_2> Cal_2s { get; set; }
}

}```

3.View

```@model Calculate_5.Models.Cal_3
@if (Model.Cal_2s.Count > 0)
{
<table cellspacing="5" border="1" width="50%" style="border-collapse:collapse;">
<tr>
<td>A</td>
<td>B</td>
<td>Ncount</td>
<td>Ra</td>
<td>Rb</td>
<td>AnB</td>
<td>A^2</td>
<td>B^2</td>
</tr>
@foreach (var item in Model.Cal_2s)
{
<tr>
<td style="text-align:center">@item.A</td>
<td style="text-align:center">@item.B</td>
<td style="text-align:center">@item.NCount</td>
<td style="text-align:center">@item.Ra</td>
<td style="text-align:center">@item.Rb</td>
<td style="text-align:center">@item.AnB</td>
<td style="text-align:center">@item.APower</td>
<td style="text-align:center">@item.BPower</td>
</tr>
}
</table>
<h2> </h2>
<table cellspacing="5" border="1" width="50%" style="border-collapse:collapse;">
<tr>
<td>Sigma_AB</td>
<td>Sigma_AxB</td>
<td>Sigma_Apower</td>
<td>Sigma_A</td>
</tr>
<tr>
<td style="text-align:center">@Model.Sigma_AB</td>
<td style="text-align:center">@Model.Sigma_AxB</td>
<td style="text-align:center">@Model.Sigma_Apower</td>
<td style="text-align:center">@Model.Sigma_A</td>
</tr>
</table>

<table cellspacing="5" border="1" width="50%" style="border-collapse:collapse;">
<tr>
<td>Value Of b</td>
</tr>
<tr>
<td style="text-align:center">@Model.Find_b</td>
</tr>
</table>
}
```

Thank.

Saturday, July 11, 2020 2:47 PM

• User1686398519 posted

Hi sy_60@yahoo.com,

In your model, you have put Cal_1 into Cal_2, Cal_2 into Cal_3, there’s no problem with it.

In controller, vm declared as Cal_2 but the type of vm_Final.Cal_2s is ICollection. It can’t assign value to  vm_Final.Cal_2s.

In view, using foreach is a great way to get each value in Cal_2s and Cal_1s.

Controller

```    public class HomeController : Controller
{
public ActionResult Index()
{
List<AB> FillData = new List<AB>  {
new AB{A=10.1m,B=20.2m},
new AB{A=20.1m,B=40.4m},
new AB{A=30.3m,B=60.6m},
new AB{A=40.4m,B=79.9m}
};
var Result = new List<Cal_1>();
int V = 0;
for (int i = 0; i < FillData.Count; i++)
{
V++;
Result.Add(new Cal_1 { A = FillData[i].A, B = FillData[i].B, NCount = V, Ra = FillData[i].A * V, Rb = FillData[i].B * V, APower = (decimal)Math.Pow((double)FillData[i].A, 2), BPower = (decimal)Math.Pow((double)FillData[i].B, 2), AnB = FillData[i].A * FillData[i].B });
}
decimal a = FillData.Sum(m => m.A);
decimal b = FillData.Sum(m => m.B);
decimal ra = Result.Sum(m => m.Ra);
decimal rb = Result.Sum(m => m.Rb);
decimal anb = Result.Sum(m => m.AnB);
decimal apower = Result.Sum(m => m.APower);
decimal bpower = Result.Sum(m => m.BPower);
{
A = a,
B = b,
NCount = V,
Ra = ra,
Rb = rb,
AnB = anb,
APower = apower,
BPower = bpower,
});
var vm = new List<Cal_2>();
{
Sigma_AB = V * anb,
Sigma_AxB = a * b,
Sigma_Apower = V * apower,
Sigma_A = (decimal)Math.Pow((double)a, 2),
Cal_1s = Result
});
var vm_Equation = new List<Cal_3>();
var vm_Final = new Cal_3();
foreach (var avm in vm)
{
vm_Equation.Add(new Cal_3 { Find_b = (avm.Sigma_AB - avm.Sigma_AxB) / (avm.Sigma_Apower - avm.Sigma_A) });
vm_Final.Find_b = (avm.Sigma_AB - avm.Sigma_AxB) / (avm.Sigma_Apower - avm.Sigma_A);
vm_Final.Cal_2s = vm;
}
return View(vm_Final);
}```

View

```@model Calculate_5.Models.Cal_3
@if (Model.Cal_2s.Count > 0)
{
<table cellspacing="5" border="1" width="50%" style="border-collapse:collapse;">
<tr>
<td>A</td>
<td>B</td>
<td>Ncount</td>
<td>Ra</td>
<td>Rb</td>
<td>AnB</td>
<td>A^2</td>
<td>B^2</td>
</tr>
@foreach (var items in Model.Cal_2s)
{
foreach(var item in items.Cal_1s)
{
<tr>
<td style="text-align:center">@item.A</td>
<td style="text-align:center">@item.B</td>
<td style="text-align:center">@item.NCount</td>
<td style="text-align:center">@item.Ra</td>
<td style="text-align:center">@item.Rb</td>
<td style="text-align:center">@item.AnB</td>
<td style="text-align:center">@item.APower</td>
<td style="text-align:center">@item.BPower</td>
</tr>
}
}
</table>
<h2> </h2>
<table cellspacing="5" border="1" width="50%" style="border-collapse:collapse;">
<tr>
<td>Sigma_AB</td>
<td>Sigma_AxB</td>
<td>Sigma_Apower</td>
<td>Sigma_A</td>
</tr>
@foreach(var item in Model.Cal_2s)
{
<tr>
<td style="text-align:center">@item.Sigma_AB</td>
<td style="text-align:center">@item.Sigma_AxB</td>
<td style="text-align:center">@item.Sigma_Apower</td>
<td style="text-align:center">@item.Sigma_A</td>
</tr>
}
</table>
<table cellspacing="5" border="1" width="50%" style="border-collapse:collapse;">
<tr>
<td>Value Of b</td>
</tr>
<tr>
<td style="text-align:center">@Model.Find_b</td>
</tr>
</table>
}```

Here is the result.

Best regards,

Yihui Sun

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Monday, July 13, 2020 6:30 AM

### All replies

• User1686398519 posted

Hi sy_60@yahoo.com,

In your model, you have put Cal_1 into Cal_2, Cal_2 into Cal_3, there’s no problem with it.

In controller, vm declared as Cal_2 but the type of vm_Final.Cal_2s is ICollection. It can’t assign value to  vm_Final.Cal_2s.

In view, using foreach is a great way to get each value in Cal_2s and Cal_1s.

Controller

```    public class HomeController : Controller
{
public ActionResult Index()
{
List<AB> FillData = new List<AB>  {
new AB{A=10.1m,B=20.2m},
new AB{A=20.1m,B=40.4m},
new AB{A=30.3m,B=60.6m},
new AB{A=40.4m,B=79.9m}
};
var Result = new List<Cal_1>();
int V = 0;
for (int i = 0; i < FillData.Count; i++)
{
V++;
Result.Add(new Cal_1 { A = FillData[i].A, B = FillData[i].B, NCount = V, Ra = FillData[i].A * V, Rb = FillData[i].B * V, APower = (decimal)Math.Pow((double)FillData[i].A, 2), BPower = (decimal)Math.Pow((double)FillData[i].B, 2), AnB = FillData[i].A * FillData[i].B });
}
decimal a = FillData.Sum(m => m.A);
decimal b = FillData.Sum(m => m.B);
decimal ra = Result.Sum(m => m.Ra);
decimal rb = Result.Sum(m => m.Rb);
decimal anb = Result.Sum(m => m.AnB);
decimal apower = Result.Sum(m => m.APower);
decimal bpower = Result.Sum(m => m.BPower);
{
A = a,
B = b,
NCount = V,
Ra = ra,
Rb = rb,
AnB = anb,
APower = apower,
BPower = bpower,
});
var vm = new List<Cal_2>();
{
Sigma_AB = V * anb,
Sigma_AxB = a * b,
Sigma_Apower = V * apower,
Sigma_A = (decimal)Math.Pow((double)a, 2),
Cal_1s = Result
});
var vm_Equation = new List<Cal_3>();
var vm_Final = new Cal_3();
foreach (var avm in vm)
{
vm_Equation.Add(new Cal_3 { Find_b = (avm.Sigma_AB - avm.Sigma_AxB) / (avm.Sigma_Apower - avm.Sigma_A) });
vm_Final.Find_b = (avm.Sigma_AB - avm.Sigma_AxB) / (avm.Sigma_Apower - avm.Sigma_A);
vm_Final.Cal_2s = vm;
}
return View(vm_Final);
}```

View

```@model Calculate_5.Models.Cal_3
@if (Model.Cal_2s.Count > 0)
{
<table cellspacing="5" border="1" width="50%" style="border-collapse:collapse;">
<tr>
<td>A</td>
<td>B</td>
<td>Ncount</td>
<td>Ra</td>
<td>Rb</td>
<td>AnB</td>
<td>A^2</td>
<td>B^2</td>
</tr>
@foreach (var items in Model.Cal_2s)
{
foreach(var item in items.Cal_1s)
{
<tr>
<td style="text-align:center">@item.A</td>
<td style="text-align:center">@item.B</td>
<td style="text-align:center">@item.NCount</td>
<td style="text-align:center">@item.Ra</td>
<td style="text-align:center">@item.Rb</td>
<td style="text-align:center">@item.AnB</td>
<td style="text-align:center">@item.APower</td>
<td style="text-align:center">@item.BPower</td>
</tr>
}
}
</table>
<h2> </h2>
<table cellspacing="5" border="1" width="50%" style="border-collapse:collapse;">
<tr>
<td>Sigma_AB</td>
<td>Sigma_AxB</td>
<td>Sigma_Apower</td>
<td>Sigma_A</td>
</tr>
@foreach(var item in Model.Cal_2s)
{
<tr>
<td style="text-align:center">@item.Sigma_AB</td>
<td style="text-align:center">@item.Sigma_AxB</td>
<td style="text-align:center">@item.Sigma_Apower</td>
<td style="text-align:center">@item.Sigma_A</td>
</tr>
}
</table>
<table cellspacing="5" border="1" width="50%" style="border-collapse:collapse;">
<tr>
<td>Value Of b</td>
</tr>
<tr>
<td style="text-align:center">@Model.Find_b</td>
</tr>
</table>
}```

Here is the result.

Best regards,

Yihui Sun

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Monday, July 13, 2020 6:30 AM
• User733430484 posted

Good learning,

Thank you very very much YihuiSun.

Tuesday, July 14, 2020 6:09 AM