LINQ to Entities DataBind中的where子句

时间:2014-01-31 19:31:43

标签: c# linq

我正在尝试将where子句添加到现有的LINQ DataBind,但我没有做任何工作。我要添加的where子句检查表refAuthSigner中是否列IsActive == 1.

这是我现有的查询:

// populates Authorized Signer dropdownlist
        using (dbPSREntities10 myEntities = new dbPSREntities10())
        {
            var allSigners = from refAuthSigner in myEntities.refAuthSigners <--- where clause somewhere around here??
                             select new
                             {
                                 refAuthSignerID = refAuthSigner.refAuthSignerID,
                                 refAuthSignerName = refAuthSigner.refAuthSignerFirst + " " + refAuthSigner.refAuthSignerLast
                             };
            ddlAuthSigners.DataSource = allSigners;
            ddlAuthSigners.DataValueField = "refAuthSignerID";
            ddlAuthSigners.DataTextField = "refAuthSignerName";
            ddlAuthSigners.DataBind();
        }

我想添加一个where子句,类似于:

 var allSigners = from refAuthSigner in myEntities.refAuthSigners 
     where refAuthSigner.IsActive == 1

这段代码不对,只是想知道如何将where子句合并到代码中。谢谢!

5 个答案:

答案 0 :(得分:2)

只需使用:

where refAuthSigner.IsActive

由于它是一个布尔值,因此无法将其与整数进行比较。它是truefalse,而不是10。 (有些语言将两者混为一谈,C#不是其中之一。)

无需将IsActive与任何内容进行比较。 where需要一个布尔值,IsActive是一个布尔值。你已经拥有了你需要的东西。

答案 1 :(得分:1)

你可以发表声明:

var allsigners = refAuthSigner.Where(x => x.refAuthSigner.IsActive) 

答案 2 :(得分:0)

试试这个:

var allSigners = from refAuthSigner in myEntities.refAuthSigners
                 where refAuthSigner.IsActive
                 select new
                 {
                     refAuthSignerID = refAuthSigner.refAuthSignerID,
                     refAuthSignerName = refAuthSigner.refAuthSignerFirst + " " + refAuthSigner.refAuthSignerLast
                 };

答案 3 :(得分:0)

Operator of '==' cannot be applied to operands of type 'bool' and 'int'. IsActive is type bit in SqlServer

如果这是错误,您将尝试使用Any代替Where,因为它返回bool

答案 4 :(得分:-1)

// populates Authorized Signer dropdownlist
        using (dbPSREntities10 myEntities = new dbPSREntities10())
        {
            var allSigners = from refAuthSigner in myEntities.refAuthSigners
                             where refAuthSigner.IsActive
                             select new
                             {
                                 refAuthSignerID = refAuthSigner.refAuthSignerID,
                                 refAuthSignerName = refAuthSigner.refAuthSignerFirst + " " + refAuthSigner.refAuthSignerLast
                             };
            ddlAuthSigners.DataSource = allSigners;
            ddlAuthSigners.DataValueField = "refAuthSignerID";
            ddlAuthSigners.DataTextField = "refAuthSignerName";
            ddlAuthSigners.DataBind();
        }