2

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();





        }
    }
LCJ
  • 22,196
  • 67
  • 260
  • 418

5 Answers5

8

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");
Habib
  • 219,104
  • 29
  • 407
  • 436
1
        var query = from o in db.Clubs
                    where o.ClubName == "club1"
                    select o;
Shai
  • 25,159
  • 9
  • 44
  • 67
1

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;
albertjan
  • 7,739
  • 6
  • 44
  • 74
1

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.

Shaiju T
  • 6,201
  • 20
  • 104
  • 196
0
var query = from o in db.Clubs
            where o.ClubName == "club1"
            select o;
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • Just FYI... indenting with four spaces is meant for code blocks only. Please don't make your entire text a code block. People have edited some of your previous posts but it looks like you haven't gotten the hint. See http://stackoverflow.com/editing-help to learn how to format your posts, and please use the edit link to fix them. – BoltClock Jul 25 '12 at 07:57