【问题标题】:Using "where": Cannot convert lambda expression to type 'bool'使用“where”:无法将 lambda 表达式转换为“bool”类型
【发布时间】:2023-03-07 14:32:01
【问题描述】:

我有如下所示的实体框架代码。在 where 条件下出现以下错误。

无法将 lambda 表达式转换为类型 'bool',因为它不是委托类型

如何克服这个错误?这个错误的原因是什么?

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





        }
    }

【问题讨论】:

  • p 应该是什么?在我看来,您在 LINQ 查询中使用了 o 的错字。
  • 为什么你在你的条件下硬编码"club1"而不是使用clubName参数?

标签: c# .net linq


【解决方案1】:

而不是where (p =&gt; p.ClubName == "club1") 使用:

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

你可能对方法链感到困惑:

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

【讨论】:

  • 更可能是... where o.ClubName == "club1"
  • 谢谢。假设我使用 FirstOrDefault 而不是 WHERE。我们如何对 Club 对象进行“var 查询”?
  • var query = db.Clubs.FirstOrDefault(p =&gt; p.ClubName == "club1");
【解决方案2】:
        var query = from o in db.Clubs
                    where o.ClubName == "club1"
                    select o;

【讨论】:

  • 哈哈,我只是在想自己:-)
【解决方案3】:

=&gt; 语法用于方法链表示法。您可能还想使用clubName 变量而不是"club1"

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

与此相同(这是您查询的正确语法):

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

【讨论】:

  • 你是对的,但是(与其他答案不同)并没有说明正确的语法是什么。
  • 完全正确。 :) 这将使它以相同的答案回答 3 次:P
【解决方案4】:

我尝试在 asp mvc Razor 中:

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

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

 }

无法将 lambda 表达式转换为类型“bool”,因为它不是 委托类型

解决方案:

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

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

}

希望能帮助别人。

【讨论】:

  • @shaijt 非常感谢它对我帮助很大
【解决方案5】:
var query = from o in db.Clubs
            where o.ClubName == "club1"
            select o;

【讨论】:

  • 仅供参考...缩进四个空格仅适用于代码块。请不要让您的整个文本成为代码块。人们已经编辑了您以前的一些帖子,但您似乎没有得到提示。请参阅stackoverflow.com/editing-help 了解如何格式化您的帖子,并请使用编辑链接来修复它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多