LCJ
LCJ

Reputation: 22661

Using "where": Cannot convert lambda expression to type 'bool'

I have entity framework code as shown below.I am getting following error in where condition.

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

How to overcome this error? What is the reason for this error?

    static void Main(string[] args)
    {

        ClubCreation();
        List<Club> selectedClubs = GetClubs("club1");

    }

    public static void ClubCreation()
    {

        string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30";
        using (var db = new NerdDinners(connectionstring))
        {

            Club club1 = new Club();
            club1.ClubName = "club1";

            Club club2 = new Club();
            club2.ClubName = "club2";

            Club club3 = new Club();
            club3.ClubName = "club3";

            db.Clubs.Add(club1);
            db.Clubs.Add(club2);
            db.Clubs.Add(club3);

            int recordsAffected = db.SaveChanges();


        }
    }

    public static List<Club> GetClubs(string clubName)
    {
        string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30";
        using (var db = new NerdDinners(connectionstring))
        {

            Club club1 = new Club();
            Club club2 = new Club();
            Club club3 = new Club();


            var query = from o in db.Clubs
                        where (p => p.ClubName == "club1")
                        select o;

            return query.ToList();





        }
    }

Upvotes: 2

Views: 10730

Answers (5)

Shaiju T
Shaiju T

Reputation: 6607

In asp mvc Razor while i tried:

@if (modelItem => item.Id == 1)
 {

<span class="badge progressbar-success">Approved</span> 

 }

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

Solution:

@if (Model.FirstOrDefault().Id == 1)
{

<span class="badge progress-bar-success">Approved</span>

}

Hope helps someone.

Upvotes: 1

albertjan
albertjan

Reputation: 7817

The => syntax is used in the method chain notation. You probably also want to use the clubName variable instead of "club1".

var query = db.Clubs.Where (p => p.ClubName == clubName);

which does the same as this (which is the correct syntax for your query):

var query = from o in db.Clubs
            where o.ClubName == clubName
            select o;

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

var query = from o in db.Clubs
            where o.ClubName == "club1"
            select o;

Upvotes: 0

Shai
Shai

Reputation: 25595

        var query = from o in db.Clubs
                    where o.ClubName == "club1"
                    select o;

Upvotes: 1

Habib
Habib

Reputation: 223402

Instead of where (p => p.ClubName == "club1") use:

var query = from o in db.Clubs
            where  o.ClubName == "club1"
            select o;

May be you are confused with method chaining where it would be:

var query = db.Clubs.Where(p => p.ClubName == "club1");

Upvotes: 8

Related Questions