.net 需要帮助解决LINQ中And运算符的谓词错误吗
我陷入了谓词和运算符的问题。代码是:.net 需要帮助解决LINQ中And运算符的谓词错误吗,.net,linq,expression,predicate,.net,Linq,Expression,Predicate,我陷入了谓词和运算符的问题。代码是: 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)) { pre
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"]));
}
}
SQLDBDataContext sqlDS=newsqldbdatacontext();
表达式pred=null//删除要开始的谓词。
if(Request[“Name”!=null&!Request[“Name”].Equals(string.Empty))
{
pred=c=>(c.ContactFirst.Contains(请求[“名称])| | c.ContactLast.Contains(请求[“名称]);
}
if(Request[“Company”!=null&!Request[“Company”].Equals(string.Empty))
{
如果(pred==null){
pred=(c=>c.Company.Contains(请求[“公司]));
}
否则{
pred=pred.And(c=>c.Company.Contains(请求[“公司]));
}
}
错误是第行:[else{pred=pred.And(c=>]
方法“”和“”没有重载,采用“1”参数
谁能告诉我如何使用.和运算符来谓词
提前感谢。和
是二进制And运算符;您的意思是表达式。AndAlso
,即
pred = Expression.AndAlso(pred, {new bit})
然而,我怀疑你做这件事很难。使用以下东西更容易:
IQueryable<Foo> source = ...
if(condition1) {
source = source.Where(predicate1);
}
if(condition2) {
source = source.Where(predicate2);
}
IQueryable源=。。。
如果(条件1){
source=source.Where(谓词1);
}
如果(条件2){
source=source.Where(谓词2);
}
例如:
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));
}
IQueryable源=。。。
字符串名称=请求[“名称”];
如果(!string.IsNullOrEmpty(名称)){
source=source.Where(user=>user.ContactFirst.Contains(name)
||user.ContactLast.Contains(name));
}
字符串company=请求[“company”];
如果(!string.IsNullOrEmpty(公司)){
source=source.Where(user=>user.Company.Contains(Company));
}
和
是二进制And运算符;您的意思是表达式
pred = Expression.AndAlso(pred, {new bit})
然而,我怀疑你做这件事很难。使用以下东西更容易:
IQueryable<Foo> source = ...
if(condition1) {
source = source.Where(predicate1);
}
if(condition2) {
source = source.Where(predicate2);
}
IQueryable源=。。。
如果(条件1){
source=source.Where(谓词1);
}
如果(条件2){
source=source.Where(谓词2);
}
例如:
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));
}
IQueryable源=。。。
字符串名称=请求[“名称”];
如果(!string.IsNullOrEmpty(名称)){
source=source.Where(user=>user.ContactFirst.Contains(name)
||user.ContactLast.Contains(name));
}
字符串company=请求[“company”];
如果(!string.IsNullOrEmpty(公司)){
source=source.Where(user=>user.Company.Contains(Company));
}
一个漂亮的小库是C#3.0中的谓词生成器(简而言之,伙计们)一个漂亮的小库是C#3.0中的谓词生成器(简而言之,伙计们)——