Aniket Kamble
Aniket Kamble

Reputation: 19

Cannot convert lambda expression to type “string” because it is not a delegate type?

       var viewModel = new CoachIndexData();

        viewModel.Coaches = db.Coaches
            .Include(i => i.Courses.Select(c => c.Department))
            .OrderBy(i => i.LastName);

I Can not Understand how to do it. I am new in ASP.NET MVC

Upvotes: 1

Views: 13223

Answers (3)

Er. Binod Mehta
Er. Binod Mehta

Reputation: 63

I have Same Problem and I solved with following

using System.Linq.Dynamic;
using System.Linq;

code here..

public IPagedList<Inv_AMC> Gets(int? page, int? pageSize, string sort = "Id", string sortdir = "DESC", string searchKey = "")
        {
            int pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;
            int pageSizeValue = pageSize.HasValue ? Convert.ToInt32(pageSize) : 10;
            return dbSet.Where(x => (x.Customer.CustomerName.ToUpper().Contains(searchKey.ToUpper()) || searchKey == ""))
                  .OrderBy(sort + " " + sortdir)
                  .ToPagedList(pageIndex, pageSizeValue);
        }

Where Inv_AMC and Customer is db object and references each other.

Upvotes: 1

silviagreen
silviagreen

Reputation: 1729

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

This worked for me.

The Include() method with Entity Framework 4.1 has extension methods and it also accepts a lambda expression. So

context.CustomerSites.Include(c => c.Customer);

is perfectly valid, all you need to do is use this:

using System.Data.Entity;

Upvotes: 6

user61470
user61470

Reputation:

Your issue lies on this line:

.Include(i => i.Courses.Select(c => c.Department))

The Include method, as described in the documentation, expects a string parameter not a lambda expression.

You need to provide Include with the path to "navigate" (i.e. how the navigation properties are set up on your context) to the required information. In your case I believe this would be:

.Include("Department")

Hope that helps.

Upvotes: 1

Related Questions