[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Cannot convert lambda expression to type 'string' because it is not a delegate type

Posted on 2014-04-04
3
Medium Priority
?
4,202 Views
Last Modified: 2014-04-07
I am creating an MVC4 application using TDD and EF v5.  I am creating my model and allowing EF to create the database.  Because I am using TDD I am using a Repository Pattern.  The problem I am having is I can't use a lamda expression to include another table.  The lamda works for OrderBy or Where but not Include.

Here is how everything is setup:
AppCatalog_Models (as created by EF) - this is the table that I am having trouble with:
namespace AppCatalog.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class AppCatalog_Database
    {
        public AppCatalog_Database()
        {
            this.AppCatalog_DBConnection = new HashSet<AppCatalog_DBConnection>();
            this.AppCatalog_PIIData = new HashSet<AppCatalog_PIIData>();
            this.AppCatalog_Application = new HashSet<AppCatalog_Application>();
        }
    
        public int databaseID { get; set; }
        public string databaseName { get; set; }
        public string databaseDescription { get; set; }
        public int databaseServerTypeID { get; set; }
    
        public virtual AppCatalog_ServerType AppCatalog_ServerType { get; set; }
        public virtual ICollection<AppCatalog_DBConnection> AppCatalog_DBConnection { get; set; }
        public virtual ICollection<AppCatalog_PIIData> AppCatalog_PIIData { get; set; }
        public virtual ICollection<AppCatalog_Application> AppCatalog_Application { get; set; }
    }
}

Open in new window


The Interface that I created:
IAppCatalogContainger
namespace AppCatalog.Models.Repository
{
    public interface IAppCatalogContainer 
    {
        IDbSet<AppCatalog_Database> AppCatalog_Database { get; set; }
        IDbSet<AppCatalog_ServerType> AppCatalog_ServerType { get; set; }
        IDbSet<AppCatalog_DBConnection> AppCatalog_DBConnection { get; set; }
        IDbSet<AppCatalog_Environment> AppCatalog_Environment { get; set; }
        IDbSet<AppCatalog_PIIData> AppCatalog_PIIData { get; set; }
        IDbSet<AppCatalog_Application> AppCatalog_Application { get; set; }
        IDbSet<AppCatalog_ApplicationTable> AppCatalog_ApplicationTable { get; set; }

        int SaveChanges();
        DbEntityEntry Entry(Object entity);
        void Dispose();
     }
}

Open in new window


Implementation of the Interface Repository:
IAppCatalogContainerRepository
using System;
using System.Collections.Generic;

namespace AppCatalog.Models.Repository
{
    public interface IAppCatalogContainerRepository
    {
        int SaveChanges();

        //******************************************************************************************************************
        // AppCatalog_Database methods
        //******************************************************************************************************************
        void CreateNewDatabase(AppCatalog_Database databaseToCreate);
        AppCatalog_Database GetDatabaseByID(int id);
        IEnumerable<AppCatalog_Database> GetAllDatabases();
        bool IsDatabaseNameUnique(int serverType, string databaseName, int databaseID);
        void SaveChanges(AppCatalog_Database dataToUpdate);

        //******************************************************************************************************************
        // AppCatalog_ServerType methods
        //******************************************************************************************************************
        IEnumerable<AppCatalog_ServerType> GetAllServerTypes();

        //******************************************************************************************************************
        // AppCatalog_Environment methods
        //******************************************************************************************************************
        IEnumerable<AppCatalog_Environment> GetAllEnvironments();

        //******************************************************************************************************************
        // AppCatalog_DBConnections methods
        //******************************************************************************************************************
        void CreateNewDBConnection(AppCatalog_DBConnection dbconnectionToCreate);
        AppCatalog_DBConnection GetDBConnectionByID(int id);
        IEnumerable<AppCatalog_DBConnection> GetAllDBConnectionsForDatabase(int databaseID);
        bool IsUserNameUnique(int environmentID, string userName, int dbconnectionID);
        void SaveChanges(AppCatalog_DBConnection dataToUpdate);

    }
}

Open in new window


The EF implementation of the Repository Interface:
EF_AppCatalogContainerManagerRepository
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.Data;

namespace AppCatalog.Models.Repository
{
    public class EF_AppCatalogContainerManagerRepository : AppCatalog.Models.Repository.IAppCatalogContainerRepository
    {
        private AppCatalogContainer _db = new AppCatalogContainer();

        public int SaveChanges()
        {
            return _db.SaveChanges();
        }


        //******************************************************************************************************************
        // AppCatalog_Database methods
        //******************************************************************************************************************
        public AppCatalog_Database GetDatabaseByID(int id)
        {
            return _db.AppCatalog_Database.FirstOrDefault(d => d.databaseID == id);
        }

        public IEnumerable<AppCatalog_Database> GetAllDatabases()
        {
            var appcatalog_database = _db.AppCatalog_Database
                                        .Include(a => a.AppCatalog_ServerType)
                                        .OrderBy(a => a.AppCatalog_ServerType.servertypeName).ThenBy(a => a.databaseName);
            //return appcatalog_database.ToList();
            return _db.AppCatalog_Database.ToList();
        }

        public void CreateNewDatabase(AppCatalog_Database databaseToCreate)
        {
            _db.AppCatalog_Database.Add(databaseToCreate);
            _db.SaveChanges();
        }

        public bool IsDatabaseNameUnique(int serverTypeID, string databaseName, int databaseID)
        {
            IEnumerable<AppCatalog_Database> dbRecords = _db.AppCatalog_Database.Where(a => a.databaseServerTypeID == serverTypeID 
                                                                                        && a.databaseName.ToUpper().Trim() == databaseName.ToUpper().Trim());
            int countRecords = dbRecords.Count();
            if (countRecords == 0)
                return true;            // Record not found for server type and database name

            // Expect 0 to 1 record to be returned.  If more than one record returned, throw an error
            if (countRecords > 1)
                throw new ValidationException("More than 1 record was returned");

            // Record found, verify record returned is not for the record being modified 
            if (databaseID != 0)
            {
                if (dbRecords.FirstOrDefault().databaseID == databaseID)
                {
                    return true;        // Found record being modified (server type and database name were not changed)
                }
                else
                {
                    return false;
                }
            }
            return false;
        }

        public void SaveChanges(AppCatalog_Database dataToUpdate)
        {
            _db.Entry(dataToUpdate).State = EntityState.Modified;
            SaveChanges();
        }

        //******************************************************************************************************************
        // AppCatalog_ServerType methods
        //******************************************************************************************************************
        public IEnumerable<AppCatalog_ServerType> GetAllServerTypes()
        {
            return _db.AppCatalog_ServerType.ToList();
        }

        //******************************************************************************************************************
        // AppCatalog_Environment methods
        //******************************************************************************************************************
        public IEnumerable<AppCatalog_Environment> GetAllEnvironments()
        {
            return _db.AppCatalog_Environment.ToList();
        }

        //******************************************************************************************************************
        // AppCatalog_DBConnection methods
        //******************************************************************************************************************
        public AppCatalog_DBConnection GetDBConnectionByID(int id)
        {
            return _db.AppCatalog_DBConnection.FirstOrDefault(d => d.dbconnectionID == id);
        }

        public IEnumerable<AppCatalog_DBConnection> GetAllDBConnectionsForDatabase(int databaseID)
        {
            return _db.AppCatalog_DBConnection.Where(d => d.dbconnectionDatabaseID == databaseID).ToList();
        }

        public void CreateNewDBConnection(AppCatalog_DBConnection dbconnectionToCreate)
        {
            _db.AppCatalog_DBConnection.Add(dbconnectionToCreate);
            _db.SaveChanges();
        }

        public bool IsUserNameUnique(int environmentID, string userName, int dbconnectionID)
        {
            IEnumerable<AppCatalog_DBConnection> dbRecords = _db.AppCatalog_DBConnection.Where(a => a.dbconnectionEnvironmentID == environmentID
                                                                                        && a.dbconnectionUserName.ToUpper().Trim() == userName.ToUpper().Trim());
            int countRecords = dbRecords.Count();
            if (countRecords == 0)
                return true;            // Record not found for environment and user name

            // Expect 0 to 1 record to be returned.  If more than one record returned, throw an error
            if (countRecords > 1)
                throw new ValidationException("More than 1 record was returned");

            // Record found, verify record returned is not for the record being modified 
            if (dbconnectionID != 0)
            {
                if (dbRecords.FirstOrDefault().dbconnectionID == dbconnectionID)
                {
                    return true;        // Found record being modified (environment and user name were not changed)
                }
                else
                {
                    return false;
                }
            }
            return false;
        }

        public void SaveChanges(AppCatalog_DBConnection dataToUpdate)
        {
            _db.Entry(dataToUpdate).State = EntityState.Modified;
            SaveChanges();
        }

    }
}

Open in new window


I am having trouble with the following routine in the EF_AppCatalogContainerManagerRepository:
        public IEnumerable<AppCatalog_Database> GetAllDatabases()
        {
            var appcatalog_database = _db.AppCatalog_Database
                                        .Include(a => a.AppCatalog_ServerType)
                                        .OrderBy(a => a.AppCatalog_ServerType.servertypeName).ThenBy(a => a.databaseName);
            //return appcatalog_database.ToList();
            return _db.AppCatalog_Database.ToList();
        }

Open in new window


I am getting the "Cannot convert lambda expression to type 'string' because it is not a delegate type" error on the .Include(a => a.AppCatalog_ServerType).  Intellisense doesn't show AppCatalog_ServerType as being valid yet it works in the OrderBy.

What changes do I need to make in order to get the Include to load the AppCatalog_ServerType table when the AppCatalog_Database table is loaded?  Any help is greatly appreciated!
0
Comment
Question by:dyarosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 39979537
Make sure that you have included the following using statement.

using System.Data.Entity;

See this link for examples
DbExtensions.Include<T, TProperty> Method (IQueryable<T>, Expression<Func<T, TProperty>>)
0
 

Author Closing Comment

by:dyarosh
ID: 39982834
Awesome!  Thank you!!
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39982904
Not a problem, glad I was able to help.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question