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);
Result.Add(new Cal_1
{
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; }
}
}
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
18 Points
56 Posts
ICollection in different model
Jul 11, 2020 02:47 PM|sy_60@yahoo.com|LINK
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;
Here is the code.
1.Controller
2.Model
3.View
Thank.
Contributor
2690 Points
771 Posts
Re: ICollection in different model
Jul 13, 2020 06:30 AM|YihuiSun|LINK
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); Result.Add(new Cal_1 { A = a, B = b, NCount = V, Ra = ra, Rb = rb, AnB = anb, APower = apower, BPower = bpower, }); var vm = new List<Cal_2>(); vm.Add(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>(); 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
Here is the result.
Best regards,
Yihui Sun
Member
18 Points
56 Posts
Re: ICollection in different model
Jul 14, 2020 06:09 AM|sy_60@yahoo.com|LINK
Good learning,
Thank you very very much YihuiSun.