views:

23

answers:

1

Hi

I would like to use Linq and strongly typed views in the right way. at the moment I do the following:

Make a Model to verify agianst:

public class Menu
    {
        public int Id { get; private set; }
        public string Text { get; private set; }
        public string Action { get; private set; }
        public string Controller { get; private set; }
        public string Parameter { get; private set; }
        public string Langue { get; private set; }

        public Menu(int id, string controller, string action, string parameter, string text)
        {
            Id = id;
            Controller = controller;
            Action = action;
            Text = text;
            Parameter = parameter;
        }

Use Linq to get the data from the database into the model:

public static List<Menu> GetTabListForMenu(string langue)
{
    Page_dbEntities entity = new Page_dbEntities();


   var tabList = (from ml in entity.wpmenulangue
                   where ml.Langue == langue
                   from m in entity.wpmenu
                   where ml.Menu == m.Id
                   from l in entity.wplangue
                   where ml.Langue == l.Langue
                   from p in entity.wppage
                   where p.Id == m.Page
                   select new { m.Id, p.Controller, p.Action, p.Parameter, ml.Text}).ToList();

    List<Menu> menu = new List<Menu>();
    foreach (var item in tabList)
    {
        menu.Add(new Menu(item.Id, item.Controller, item.Action, item.Parameter, item.Text));
    }
    return menu;
}

I am pretty convinced that this is not the optimal way to do this and have 2 questions:

  1. When I get the data from the database I first use a var and then have to move it to the object with a foreach. this seems like a waste of both my time and less effeicent then getting it with sql.

  2. I have been told that I can just verify up agianst the entitymodel. Even if i use multiple entities in a view. is this true? (the one telling me this wes not able to get it to work and I have not been able to find anything about it online).

I will try to look back on this post in the next couple of hours, but might have to wait 24 hours.

+1  A: 
public static List<Menu> GetTabListForMenu(string langue)
{
    Page_dbEntities entity = new Page_dbEntities();
    return (from ml in entity.wpmenulangue
            where ml.Langue == langue
            from m in entity.wpmenu
            where ml.Menu == m.Id
            from l in entity.wplangue
            where ml.Langue == l.Langue
            from p in entity.wppage
            where p.Id == m.Page
            select new Menu(m.Id, p.Controller, p.Action, p.Parameter, ml.Text)
    ).ToList();
}

As for the validation is concerned you shouldn't use multiple entities in the view. You should use a single entity which is called ViewModel. This ViewModel is a class that represents the data on the view. If you are using DataAnnotations for validation you could decorate this view model properties with attributes that indicate how to be validated.

Darin Dimitrov
Already tried that ;) it gives the following fault:Only parameterless constructors and initializers are supported in LINQ to Entities. thx for the link to DataAnnotations though, I still need to read up on how to make probber validation :)
Jorn
You need to have a default constructor for your view models. In your case the Menu class constructor takes parameters. You will need to remove this constructor and initialize class using C# 3.0 class initializers syntax.
Darin Dimitrov
Thanks, that solved my problem :)
Jorn