需要帮助解决 LINQ 中 And 运算符的谓词错误
我遇到了谓词和运算符的问题。 代码是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
And
是二元 And 运算符; 你的意思是Expression.AndAlso
,即但是,我怀疑你正在以困难的方式做到这一点。 使用以下内容更容易:
例如:
And
is the binary And operator; you meanExpression.AndAlso
, i.e.However, I suspect you are doing this the hard way. It is easier to use things like:
For example:
一个漂亮的小库是 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