1

I am trying to allow my users to construct their own queries. I found this code which seems to solve this problem:

var predicate = PredicateBuilder.True<T1>();

if (a != "")
    predicado = predicado.And(e1 => e1.field1.Contains(a));

if (b!= "")
    predicado = predicado.And(e1 => e1.field2.Contains(b));

var TestQuery = db.T1.AsExpandable().Where(predicate).ToList(); 

These are my model classes:

public partial class T1
{
    public long id_t1 { get; set; }

    public string field1 { get; set; }

    public string field2 { get; set; }
}

public partial class T1
{
    public long id_t2 { get; set; }

    public string field1 { get; set; }

    public string field2 { get; set; }
}

But i need a way to use the PredicateBuilder in a join statement like this one:

var query = from f in db.T1
    where (f.T1field1.Equals("something") && f.T1field2.Equals("something"))
    join m in db.T2 on f.T1field1 equals m.T2field1
    where (m.T2fiel1 != null && m.T2field2 != null )
    select m;
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
David
  • 11
  • 4
  • ups when i say T2field1 i am talking about the column field1 in the table T2 – David May 07 '15 at 00:53
  • and the same goes for the T1field1 – David May 07 '15 at 00:54
  • In order for this to work with Linq to Sql (and Linq with EF also needs to go to Sql in the end), your predicates have to be available in expressions: See http://stackoverflow.com/a/19618035/49251 and http://stackoverflow.com/a/29871197/49251. – DWright May 07 '15 at 01:35
  • eureka , this way work for me: – David May 11 '15 at 20:37
  • 'var query = from f in db.T1' ' where (f.T1field1.Equals("something") && f.T1field2.Equals("something")) join m in db.T2 on f.T1field1 equals m.T2field1 where (m.T2fiel1 != null && m.T2field2 != null ) select m; – David May 11 '15 at 20:37
  • sorry is this way var query = from f in db.T1.AsExpandable().Where (predicado) join m in db.T2 on f.T1field1 equals m.T2field1.AsExpendable().Where(predicado) select m; – David May 11 '15 at 20:39
  • and the predicado most be like this – David May 11 '15 at 20:41
  • 'var predicado = PredicateBuilder.True();' 'if (a != "")' 'predicado = predicado.And(e1 => e1.field1.Contains(a));' – David May 11 '15 at 20:41

0 Answers0