C# Template – Custom Query in Model

//if you have innter joins or anything
   //this is an example if you want a custom query, especially thos with inner joins.
        public DataTable GetUsersWhereAdmin()  //you can pass an argument exanmple the "user_type" for the where
        {
            string query = @"
            SELECT 
                u.id AS Id,
                u.user_name AS UserName,
                u.first_name AS FirstName,
                r.last_name AS LastName,
                r.user_type UserType,
                r.is_active IsActive
            FROM users_local u
            LEFT JOIN user_access r ON u.role_id = r.id
            WHERE u.user_type = ?user_type AND u.is_active = ?is_active";

            var where = new Dictionary<string, object>{
                { "user_type", "Administrator" },
                { "is_active", "Y" }
            };

            return View(query, where);
        }

        //this is an example if you want a custom query, especially thos with inner joins.
        public DataTable GetAllUsers()  //no where
        {
            string query = @"
            SELECT 
                u.id AS Id,
                u.user_name AS UserName,
                u.first_name AS FirstName,
                u.last_name AS LastName,
                u.user_type UserType,
                u.is_active IsActive
            FROM users_local u";
          
          
            return View(query);
        }