I have imported a database that has collection MainData in a one to many relationship with Subdata, the generated code looks something like this;
public DbSet<MainData> MainDatas{ get; set; }
public partial class MainData
{
public string NameOfMainData{ get; set; }
public virtual ICollection<SubData> SubDataSet{ get; set; }
}
SubData just has some noncomplex datatypes like strings and ints.
I can import the data to the view so i know that that part works. I want to merge this into a model so that i have the "NameOfMainData, and a collection of SubData in the same model for easy acsess in the view, but i dont get it to work, here is my try;
Model;
public class DataIWantInAList
{
public IList<DataIWant> DataList { get; set; }
}
public class DataIWant
{
public string NameOfMainData{ get; set; }
public IEnumerable<SubData> SubDataSet{ get; set; }
}
In the Controller i thought that i could add items in the DataList but i did not manage to find anything on the internet or work it out for my self... And this;
var DataToView= from p in db.MainData
select new DataIWant()
{
NameOfMainData= p.NameOfMainData,
SubDataSet= p.SubDataSet
};
return View(DataToView);
Dont work as excpected. Im not so good in LINQ but it feels like this would be pretty easy and that makes it so frustrating when i dont get it to work.
Any input is appriciated.