Solved

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

Posted on 2014-04-04
3
3,939 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
  • 2
3 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 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 62

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now