public Data Get(View view)
 {
     Data data = new Data();
     data.ViewId = view.ViewId;
     List<Felt> fields = new List<Felt>();
     Form form = db.Forms.SingleOrDefault(d => d.FormId == view.Group.DefaultFormId);
     if (form != null)
     {
         data.SRS = form.SRS;
         data.FormId = form.FormId;
         foreach (Field field in form.Fields.OrderBy(m => m.FieldOrder))
         {
             Felt felt = new Felt()
             {
                 Data = field.Data,
                 Name = field.Name,
                 Required = field.Required,
                 TypeId = field.FieldTypeId,
                 Id = field.FieldId
             };
             List<Udvælgelse> selections = new List<Udvælgelse>();
             int i = 0;
             foreach (Models.Selection selection in field.Selections.OrderBy(m => m.SelectionOrder))
             {
                 i++;
                 Udvælgelse udvælgelse = new Udvælgelse();
                 udvælgelse.Name = selection.Name;
                 udvælgelse.Id = field.FieldId.ToString() + "-" + i.ToString();
                 if (selection.Name == field.Data)
                     udvælgelse.Selected = true;
                 else
                     udvælgelse.Selected = false;
                 selections.Add(udvælgelse);
             }
             felt.Selections = selections;
             Permission permission = view.Permissions.SingleOrDefault(m => m.FieldId == field.FieldId);
             if (permission != null)
             {
                 felt.Permission = permission.PermissionTypeId;
             }
             else
                 felt.Permission = 3;
             fields.Add(felt);
         }
     }
     data.Felter = fields;
     return data;
 }
 //
 // Insert/Delete Methods
 public void Add(View item)
 {
     db.Views.Add(item);
 }
 public void Delete(View item)
 {
     db.Views.Remove(item);
 }
        public DataListModel GetList(View view, string userId)
        {
            RepositoryCompanies repositoryCompanies = new RepositoryCompanies();
            List<Field> columns = new List<Field>();
            Form form = view.Group.Forms.Single(m => m.FormId == view.Group.DefaultFormId);
            string select = "";
            bool fId = false;
            bool fUserId = false;
            bool fUniqueId = false;
            bool fDato = false;
            foreach (Field field in form.Fields.Where(a => a.FieldColumn != null && !a.FieldColumn.Equals(String.Empty)).OrderBy(b => b.FieldOrder))
            {
                Permission permission = view.Permissions.FirstOrDefault(a => a.FieldId == field.FieldId);
                if (permission == null || permission.PermissionTypeId != 0)
                {
                    columns.Add(field);
                    switch (field.FieldColumn.ToUpper())
                    {
                        case "ID":
                            select = select + "," + field.FieldColumn;
                            fId = true;
                            break;
                        case "UNIQUEID":
                            select = select + "," + field.FieldColumn;
                            fUniqueId = true;
                            break;
                        case "USERID":
                            select = select + "," + field.FieldColumn;
                            fUserId = true;
                            break;
                        case "DATO":
                            select = select + "," + field.FieldColumn;
                            fDato = true;
                            break;
                        default:
                            if (field.FieldTypeId == 16)
                            {
                                select = select + "," + field.FieldColumn + ".STAsText() AS " + field.FieldColumn;
                            }
                            else
                            {
                                select = select + "," + field.FieldColumn;
                            }

                            break;
                    }

                }

            }
            if (!fUniqueId)
                select = select + ",UniqueId";
            if (!fUserId)
                select = select + ",UserId";
            if (!fDato)
                select = select + ",Dato";
            if (!fId)
                select = select + ",Id";
            select = "SELECT " + select.Substring(1);

            DataListModel model = new DataListModel
            {
                View = view,
                Form = form,
                Columns = columns
            };
            DataTable dt = new DataTable();
            using (SqlConnection connection = new SqlConnection(form.ResourceName))
            {
                SqlCommand command = connection.CreateCommand();
                command.CommandText = select+" FROM " + form.ClassName;
                if (!String.IsNullOrEmpty(userId))
                {
                    command.CommandText = command.CommandText + " WHERE UserId = '" + userId + "'";
                }
                command.CommandText += " order by dato desc";
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                adapter.Fill(dt);
            }
            Dictionary<string, UserInfo> users = new Dictionary<string, UserInfo>();
            foreach (DataRow row in dt.Rows)
            {
                DataListItemModel listItem = new DataListItemModel();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (!row.IsNull(i))
                    {

                        string propertyName = dt.Columns[i].ColumnName;
                        string propertyNameUpper = propertyName.ToUpper();
                        switch (propertyNameUpper)
                        {
                            case "ID":
                                listItem.ItemId = (int)row[i];
                                if(fId)
                                    listItem.Data.Add(propertyName, row[i].ToString());
                                break;
                            case "UNIQUEID":

                                listItem.UniqueId = (string)row[i];
                                if (fUniqueId)
                                    listItem.Data.Add(propertyName, row[i].ToString());
                                break;
                            case "USERID":
                                if (row[i] != DBNull.Value && row[i].ToString() != "")
                                {
                                    listItem.UserId = (string)row[i];
                                    if (fUserId)
                                        listItem.Data.Add(propertyName, row[i].ToString());
                                    UserInfo user = null;
                                    if (users.ContainsKey(listItem.UserId))
                                        user = users[listItem.UserId];
                                    else
                                    {
                                        MembershipUser user1 = Membership.GetUser(new Guid(listItem.UserId));
                                        if (user1 != null)
                                        {
                                            user = new UserInfo();
                                            user.UserName = user1.UserName;
                                            UserProfile profile = UserProfile.GetUserProfile(user1.UserName);
                                            if (profile != null)
                                            {
                                                Company company = repositoryCompanies.Get(profile.CompanyId);
                                                if (company != null)
                                                {
                                                    user.CompanyName = company.Name;
                                                }
                                            }
                                        }
                                        users.Add(listItem.UserId, user);
                                    }
                                    if (user != null)
                                    {
                                        listItem.UserName = user.UserName;
                                        listItem.Company = user.CompanyName;
                                    }
                                }
                                break;
                            case "DATO":
                                listItem.Date = (DateTime)row[i];
                                if (fDato)
                                    listItem.Data.Add(propertyName, row[i].ToString());
                                break;
                            default:
                                listItem.Data.Add(propertyName, row[i].ToString());
                                break;
                        }
                    }
                }
                model.ListItemModels.Add(listItem);
            }
            return model;
        }