1

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.

1
  • You specifically want to know how to do this with Linq, so this isn't an answer, per se, but have you considered using AutoMapper (automapper.org)? Makes this sort of thing so much easier. Commented Apr 17, 2013 at 14:23

3 Answers 3

1

DISCLAIMER
- the data-fetch is made under the assumption you use EntityFramework as I am not the fittest in LINQ only :( please feel free to adjust it
- this is just one of many ways. try to experiment when you give the model to the view
- params is supposed to be a rudimentary placeholder for a "filter"
- you can expand the mapping in any way you like, just make sure to correctly reference in the View, else you will encounter runtime Errors

In the Model you can do something like this (even though i would move the entity-fetch to a private function. in your class DataIWant you just specify the data you want. if you want to specifically map, then do so on initiation of your DataIWantInAList

public class DataIWantInAList
{
    public IList<DataIWant> data;

    //Instanciation & Initialization
    public DataIWantInAList(params customparam)
    {
        using(EntitySet ent = new EntitySet())
        {
        //fetch data from DB
        this.data = (from x in ent.DataTableIWant where param == customparam select x).ToList();
        }
   }
}

and in your Controller you might wanna go:

public ActionResult fooBar()
{ 
  DataIWantInAList model = new DataIWantInAList(params);
  return View(model);
}

while you can get the data in your view in a manner like this:

var List = model.data;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks i will try this out, but try to move it to another function (or maybe in the controller?). :)
0

You can use .Include(...) to get the data you want, assuming I understand your question.

var DataToView = from p in db.MainData
                        select new DataIWant()
                        {
                            NameOfMainData= p.NameOfMainData.Include(p => p.SubDataSet)
                        }

Comments

0

You can use Include to load the related entities, then map them to your model type:

var DataToView= from p in db.MainData
                            .Include("ItemTypeReference")
                        select new DataIWant()
                        {
                            NameOfMainData= p.NameOfMainData,
                            SubDataSet= p.SubDataSet
                                         .Select(s => new SubData {
                                                      // map SubData fields here
                                                      })
                                         .ToList()
                        };

1 Comment

Thanks for the help! :) I think the select in the SubDataSet might do it for me, havent got it to work yet but it seems like the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.