Solved

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

Posted on 2014-04-04
3
3,888 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

11 Experts available now in Live!

Get 1:1 Help Now