需要帮助解决 LINQ 中 And 运算符的谓词错误

发布于 2024-07-12 14:12:39 字数 860 浏览 8 评论 0原文

我遇到了谓词和运算符的问题。 代码是:

SQLDBDataContext sqlDS = new SQLDBDataContext();
Expression<Func<User,bool>> pred = null; //delcare the predicate to start with.

if (Request["Name"] != null && ! Request["Name"].Equals(string.Empty))
{  
   pred = c => ( c.ContactFirst.Contains(Request["Name"]) || c.ContactLast.Contains(Request["Name"]));
}

if (Request["Company"] != null && !Request["Company"].Equals(string.Empty))
{
   if (pred == null) { 
      pred = (c => c.Company.Contains(Request["Company"])); 
   }
   else {
      pred = pred.And(c => c.Company.Contains(Request["Company"]));
   }
}

错误是行:[ else {pred = pred.And(c => ] 方法“And”没有重载需要“1”个参数

任何人都可以告诉我如何使用 .And 运算符进行谓词。

提前致谢。
安尼尔

I got into problem of predicate And operator. Code is :

SQLDBDataContext sqlDS = new SQLDBDataContext();
Expression<Func<User,bool>> pred = null; //delcare the predicate to start with.

if (Request["Name"] != null && ! Request["Name"].Equals(string.Empty))
{  
   pred = c => ( c.ContactFirst.Contains(Request["Name"]) || c.ContactLast.Contains(Request["Name"]));
}

if (Request["Company"] != null && !Request["Company"].Equals(string.Empty))
{
   if (pred == null) { 
      pred = (c => c.Company.Contains(Request["Company"])); 
   }
   else {
      pred = pred.And(c => c.Company.Contains(Request["Company"]));
   }
}

error is line : [ else {pred = pred.And(c => ]
No overload for method 'And' takes '1' arguments

Can anybody tell me how to use .And operator to predicate.

Thanks in advance.
Anil

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

合约呢 2024-07-19 14:12:39

And 是二元 And 运算符; 你的意思是Expression.AndAlso,即

pred = Expression.AndAlso(pred, {new bit})

但是,我怀疑你正在以困难的方式做到这一点。 使用以下内容更容易:

IQueryable<Foo> source = ...
if(condition1) {
    source = source.Where(predicate1);
}
if(condition2) {
    source = source.Where(predicate2);
}

例如:

IQueryable<User> source = ...
string name = Request["Name"];
if(!string.IsNullOrEmpty(name)) {
    source = source.Where(user => user.ContactFirst.Contains(name)
               || user.ContactLast.Contains(name));
}
string company = Request["Company"];
if(!string.IsNullOrEmpty(company)) {
    source = source.Where(user => user.Company.Contains(company));
}

And is the binary And operator; you mean Expression.AndAlso, i.e.

pred = Expression.AndAlso(pred, {new bit})

However, I suspect you are doing this the hard way. It is easier to use things like:

IQueryable<Foo> source = ...
if(condition1) {
    source = source.Where(predicate1);
}
if(condition2) {
    source = source.Where(predicate2);
}

For example:

IQueryable<User> source = ...
string name = Request["Name"];
if(!string.IsNullOrEmpty(name)) {
    source = source.Where(user => user.ContactFirst.Contains(name)
               || user.ContactLast.Contains(name));
}
string company = Request["Company"];
if(!string.IsNullOrEmpty(company)) {
    source = source.Where(user => user.Company.Contains(company));
}
遗忘曾经 2024-07-19 14:12:39

一个漂亮的小库是 C# 3.0 中的 Predicate Builder in a Nutshell - http:// /www.albahari.com/nutshell/predicatebuilder.aspx

A nifty little library to have is the Predicate Builder from the C# 3.0 in a Nutshell guys - http://www.albahari.com/nutshell/predicatebuilder.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文